博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Spring Boot && Spring Cloud系列】构建Springboot项目 实现restful风格接口
阅读量:5759 次
发布时间:2019-06-18

本文共 11161 字,大约阅读时间需要 37 分钟。

项目代码如下:

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 
2
4
4.0.0
5 6
com.slp
7
restSpringDemo1
8
0.1.0
9 10
11
org.springframework.boot
12
spring-boot-starter-parent
13
1.4.1.RELEASE
14
15 16
17
18
org.springframework.boot
19
spring-boot-starter-web
20
21
22
org.springframework.boot
23
spring-boot-starter-test
24
test
25
26
27
com.jayway.jsonpath
28
json-path
29
test
30
31
32 33
34
1.8
35
36 37 38
39
40
41
org.springframework.boot
42
spring-boot-maven-plugin
43
44
45
46 47
48
49
spring-releases
50
https://repo.spring.io/libs-release
51
52
53
54
55
spring-releases
56
https://repo.spring.io/libs-release
57
58
59

在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的版本设置变量。

 

转载于:https://www.cnblogs.com/dream-to-pku/p/5955862.html

你可能感兴趣的文章
创建美国地区的appleId
查看>>
例题10-2 UVa12169 Disgruntled Judge(拓展欧几里德)
查看>>
JS 原生ajax写法
查看>>
Composer管理PHP依赖关系
查看>>
React.js学习笔记之JSX解读
查看>>
Socket编程问题小记
查看>>
基于Flask-Angular的项目组网架构与部署
查看>>
一张图道尽程序员的出路
查看>>
redis 常用命令
查看>>
LVS+Keepalived高可用负载均衡集群架构
查看>>
烂泥:kvm安装windows系统蓝屏
查看>>
iPhone开发面试题--葵花宝典
查看>>
EdbMails Convert EDB to PST
查看>>
POJ 2184
查看>>
大话 程序猿 眼里的 接口
查看>>
struts2用了哪几种模式
查看>>
replace函数结合正则表达式实现转化成驼峰与转化成连接字符串的方法
查看>>
ubuntu 初学常用命令
查看>>
WCF客户端与服务端通信简单入门教程
查看>>
android 资源种类及使用
查看>>