项目代码如下:
1 package hello; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan 7 8 public class Application { 9 10 public static void main(String[] args) {11 SpringApplication.run(Application.class, args);12 }13 14 }
1 package hello; 2 3 public class Greeting { 4 private long id; 5 private String content; 6 public Greeting(long id, String content) { 7 super(); 8 this.id = id; 9 this.content = content;10 }11 public long getId() {12 return id;13 }14 public void setId(long id) {15 this.id = id;16 }17 public String getContent() {18 return content;19 }20 public void setContent(String content) {21 this.content = content;22 }23 24 }
1 package hello; 2 3 import java.util.concurrent.atomic.AtomicLong; 4 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestParam; 7 import org.springframework.web.bind.annotation.RestController; 8 9 @RestController // shorthand for @Controller and @ResponseBody rolled together10 public class GreetingController {11 12 private static final String template="Hello,%s!";13 private final AtomicLong counter=new AtomicLong();14 15 @RequestMapping("/greeting")16 public Greeting greeting(@RequestParam(value="name",defaultValue="World")String name){17 System.out.println("-------------------------");18 return new Greeting(counter.incrementAndGet(),String.format(template, name));19 }20 }
1 24 4.0.0 5 6com.slp 7restSpringDemo1 80.1.0 9 1011 15 16org.springframework.boot 12spring-boot-starter-parent 131.4.1.RELEASE 1417 32 3318 21org.springframework.boot 19spring-boot-starter-web 2022 26org.springframework.boot 23spring-boot-starter-test 24test 2527 31com.jayway.jsonpath 28json-path 29test 3034 36 37 381.8 3539 46 4740 4541 44org.springframework.boot 42spring-boot-maven-plugin 4348 5349 52spring-releases 50https://repo.spring.io/libs-release 5154 5955 58spring-releases 56https://repo.spring.io/libs-release 57
在Application.java中run as java application 出现:
1 . ____ _ __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 5 ' |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot :: (v1.4.1.RELEASE) 8 9 2016-10-13 11:19:16.510 INFO 9864 --- [ main] hello.Application : Starting Application on QH-20160418YQMB with PID 9864 (started by sanglp in D:\rest风格的apring项目\restSpringDemo1)10 2016-10-13 11:19:16.525 INFO 9864 --- [ main] hello.Application : No active profile set, falling back to default profiles: default11 2016-10-13 11:19:16.713 INFO 9864 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@72f926e6: startup date [Thu Oct 13 11:19:16 CST 2016]; root of context hierarchy12 2016-10-13 11:19:19.691 INFO 9864 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)13 2016-10-13 11:19:19.710 INFO 9864 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat14 2016-10-13 11:19:19.710 INFO 9864 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.3515 2016-10-13 11:19:20.148 INFO 9864 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.16 2016-10-13 11:19:20.148 INFO 9864 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext17 2016-10-13 11:19:20.148 INFO 9864 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3451 ms18 2016-10-13 11:19:20.382 INFO 9864 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]19 2016-10-13 11:19:20.398 INFO 9864 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]20 2016-10-13 11:19:20.398 INFO 9864 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]21 2016-10-13 11:19:20.398 INFO 9864 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]22 2016-10-13 11:19:20.398 INFO 9864 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]23 2016-10-13 11:19:20.866 INFO 9864 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@72f926e6: startup date [Thu Oct 13 11:19:16 CST 2016]; root of context hierarchy24 2016-10-13 11:19:20.986 INFO 9864 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting]}" onto public hello.Greeting hello.GreetingController.greeting(java.lang.String)25 2016-10-13 11:19:20.986 INFO 9864 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)26 2016-10-13 11:19:20.986 INFO 9864 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)27 2016-10-13 11:19:21.033 INFO 9864 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]28 2016-10-13 11:19:21.033 INFO 9864 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]29 2016-10-13 11:19:21.095 INFO 9864 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]30 2016-10-13 11:19:21.307 INFO 9864 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup31 2016-10-13 11:19:21.463 INFO 9864 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)32 2016-10-13 11:19:21.479 INFO 9864 --- [ main] hello.Application : Started Application in 6.497 seconds (JVM running for 7.83)33 2016-10-13 11:21:46.570 INFO 9864 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'34 2016-10-13 11:21:46.570 INFO 9864 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started35 2016-10-13 11:21:46.622 INFO 9864 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 51 ms
表示启动成功,这是就可以访问了 http://localhost:8080/greeting?name=xxb
会出现如下结果:
{"id":3,"content":"Hello,xuxiaobo!"}
这是一个纯java的项目,不需要启动tomcat也可以执行访问
首先你需要建立一个基本的项目,你可以使用任何你喜欢的方式去构造Spring项目,这里使用的是Maven.如果你不熟悉Maven的话请移步 .
创建项目目录结构
在你所选的项目目录中,创建如下的子目录结构;例如在unix系统中使用mkdir -p src/main/java/hello。
└── src └── main └── java └── hello
pom.xml
4.0.0 org.springframework gs-rest-service 0.1.0 org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.jayway.jsonpath json-path test 1.8 org.springframework.boot spring-boot-maven-plugin spring-releases https://repo.spring.io/libs-release spring-releases https://repo.spring.io/libs-release
Spring Boot的maven插件提供了很多便利的特性:
- 它包含了路径中的所有jar文件,使用“über-jar”。它使得执行和转换服务非常便利。
-
它查询
public static void main()
方法去标志为一个可执行的类 - 它提供了一个内置的匹配Spring boot 依赖的版本,你可以选择任何你希望的版本,但是默认情况下它会选择Boot的版本设置变量。