(一) 写在前面
(二)搭建和启动步骤并开发简单的Restful风格接口
(1) 首先,我们访问:http://start.spring.io/,选择构建工具 Maven,采用编程语言 Java,输入如图中的信息,我使用的spring-boot是2.0.5 版本,在Search for dependencies下的搜索输入框中输入Web并作为选中的依赖, 点击下面的Generate Project按钮下载项目压缩包
(2)解压项目后使用intelliJ IDEA开发工具导入到工作目录,导入过程截图如下:选择File-->New-->Project from Existing Source 从解压后的项目中导入spring-boot-demo项目,选择Maven Project file后点击OK按钮即可
(3)启动类DemoApplication.java与pom.xml为spring-io生成项目时自动生成,代码如下
ackage com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.0.0
com.example
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
(4)新建一个TestController类,并写两简单的restful风格的接口,代码如下
package com.example.demo.com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="/restful")
public class TestController {
@RequestMapping(value="/test",method= RequestMethod.GET)
public String testRestInterface(@RequestParam("name") String name){
return "Hello:" + name;
}
@RequestMapping(value="/index",produces = "text/plain;charset=utf-8")
public String index(){
return "Hello Spring Boot!";
}
}
(5) 点击Run菜单下的Debug 'DemoApplication' 启动项目
项目启动成功的日志信息如下,下面那个图案是属于spring-boot项目启动时特有的
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
2018-09-22 00:54:30.747 INFO 10808 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on heshengfu1211 with PID 10808 (D:\myproject\demo\target\classes started by HP in D:\myproject\demo)
2018-09-22 00:54:30.750 INFO 10808 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-09-22 00:54:30.803 INFO 10808 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@45ac5f9b: startup date [Sat Sep 22 00:54:30 GMT+08:00 2018]; root of context hierarchy
2018-09-22 00:54:32.036 INFO 10808 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-09-22 00:54:32.067 INFO 10808 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-09-22 00:54:32.068 INFO 10808 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-09-22 00:54:32.074 INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : Loaded APR based Apache Tomcat Native library [1.2.17] using APR version [1.6.3].
2018-09-22 00:54:32.074 INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2018-09-22 00:54:32.074 INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2018-09-22 00:54:33.138 INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.0.2o 27 Mar 2018]
2018-09-22 00:54:33.271 INFO 10808 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-09-22 00:54:33.271 INFO 10808 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2472 ms
2018-09-22 00:54:33.356 INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-09-22 00:54:33.364 INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-22 00:54:33.364 INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-22 00:54:33.364 INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-22 00:54:33.364 INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-22 00:54:33.545 INFO 10808 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:33.808 INFO 10808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@45ac5f9b: startup date [Sat Sep 22 00:54:30 GMT+08:00 2018]; root of context hierarchy
2018-09-22 00:54:33.873 INFO 10808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/restful/index],produces=[text/plain;charset=utf-8]}" onto public java.lang.String com.example.demo.com.example.demo.controller.TestController.index()
2018-09-22 00:54:33.876 INFO 10808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/restful/test],methods=[GET]}" onto public java.lang.String com.example.demo.com.example.demo.controller.TestController.testRestInterface(java.lang.String)
2018-09-22 00:54:33.879 INFO 10808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity
2018-09-22 00:54:33.879 INFO 10808 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-22 00:54:33.909 INFO 10808 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:33.909 INFO 10808 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:34.043 INFO 10808 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-09-22 00:54:34.087 INFO 10808 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-22 00:54:34.090 INFO 10808 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.786 seconds (JVM running for 4.879)
(6)postman调用GET请求的接口,请求头Headers设置Content-Type:application/json,调用过程截图如下
响应状态Status=200, Body中输出了 Hello: Spring-boot表示接口调用成功
(7) chrom浏览器中输入http://localhost:8080/restful/index 在浏览器端调用另一个返回text/plain格式用于浏览器端显示的接口,截图如下,一样调用成功
本人后续还将继续更新自己的spring-boot demo项目,下一篇博客讲开始采用JPA数据源连接mysql数据库,实现通过调用GET和POST请求接口实现数据集库数据的查询、插入、更新和删除。本文参考链接如下:
黄朝兵的达人课
轻轻松松学习SpringBoot2