激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|

服務器之家 - 編程語言 - JAVA教程 - 詳解使用Spring Boot開發Restful程序

詳解使用Spring Boot開發Restful程序

2020-10-28 14:58yzllz001 JAVA教程

本篇文章主要介紹了詳解使用Spring Boot開發Restful程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、簡介

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力于在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。

多年以來,Spring IO平臺飽受非議的一點就是大量的XML配置以及復雜的依賴管理。在去年的SpringOne 2GX會議上,Pivotal的CTO Adrian Colyer回應了這些批評,并且特別提到該平臺將來的目標之一就是實現免XML配置的開發體驗。Boot所實現的功能超出了這個任務的描述,開發人員不僅不再需要編寫XML,而且在一些場景中甚至不需要編寫繁瑣的import語句。在對外公開的beta版本剛剛發布之時,Boot描述了如何使用該框架在140個字符內實現可運行的web應用,從而獲得了極大的關注度,該樣例發表在tweet上。

Spring Boot不生成代碼,且完全不需要XML配置。其主要目標如下:

  1. 為所有的Spring開發工作提供一個更快、更廣泛的入門經驗。
  2. 開箱即用,你也可以通過修改默認值來快速滿足你的項目的需求。
  3. 提供了一系列大型項目中常見的非功能性特性,如嵌入式服務器、安全、指標,健康檢測、外部配置等。

Spring Boot官網: http://projects.spring.io/spring-boot/

二、開發環境準備

IDE:IntelliJ IDEA

官網地址:https://www.jetbrains.com/idea/download/

JDK:1.8

Maven

數據庫:MySQL

我將以一個用戶積分系統為例,開發一個Restful風格的服務端

三、第一個Restful程序

1.新建一個普通Maven工程

詳解使用Spring Boot開發Restful程序

詳解使用Spring Boot開發Restful程序

詳解使用Spring Boot開發Restful程序

詳解使用Spring Boot開發Restful程序

創建項目完成后目錄結構如下圖所示

詳解使用Spring Boot開發Restful程序

2.在POM文件中加入對Spring-Boot的依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <groupId>com.bluecoffee</groupId>
 <artifactId>mapp</artifactId>
 <version>1.0-SNAPSHOT</version>
 
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.1.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>
 
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>
 
 
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

3.新建一個RestController來接收客戶端的請求,我們來模擬一個登錄請求

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.yepit.mapp.rest;
 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
 
/**
 * Created by qianlong on 16/7/20.
 */
@RestController
public class UserController {
 
 @RequestMapping(value = "/users/{username}",method = RequestMethod.GET,consumes="application/json")
 public String getUser(@PathVariable String username, @RequestParam String pwd){
  return "Welcome,"+username;
 }}
  1. 關鍵字@RestController代表這個類是用Restful風格來訪問的,如果是普通的WEB頁面訪問跳轉時,我們通常會使用@Controller
  2. value = "/users/{username}" 代表訪問的URL是"http://host:PORT/users/實際的用戶名"
  3. method = RequestMethod.GET 代表這個HTTP請求必須是以GET方式訪問
  4. consumes="application/json" 代表數據傳輸格式是json
  5. @PathVariable將某個動態參數放到URL請求路徑中
  6. @RequestParam指定了請求參數名稱

4.新建啟動Restful服務端的啟動類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.yepit.mapp;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * Created by qianlong on 16/7/20.
 */
@SpringBootApplication
public class MappRunApplication {
 
 public static void main(String[] args) {
  SpringApplication.run(MappRunApplication.class, args);
 }
}

5.執行MappRunApplication的Main方法啟動Restful服務,可以看到控制臺有如下輸出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
. ____   _   __ _ _
 /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
 \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.3.3.RELEASE)
 
2016-07-20 16:49:43.334 INFO 2106 --- [   main] com.yepit.mapp.MappRunApplication  : Starting MappRunApplication on bogon with PID 2106 (/Users/qianlong/workspace/spring-boot-samples/target/classes started by qianlong in /Users/qianlong/workspace/spring-boot-samples)
2016-07-20 16:49:43.338 INFO 2106 --- [   main] com.yepit.mapp.MappRunApplication  : No active profile set, falling back to default profiles: default
2016-07-20 16:49:43.557 INFO 2106 --- [   main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@543e710e: startup date [Wed Jul 20 16:49:43 CST 2016]; root of context hierarchy
2016-07-20 16:49:44.127 INFO 2106 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-07-20 16:49:44.658 INFO 2106 --- [   main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-07-20 16:49:44.672 INFO 2106 --- [   main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-07-20 16:49:44.673 INFO 2106 --- [   main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.32
2016-07-20 16:49:44.759 INFO 2106 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring embedded WebApplicationContext
2016-07-20 16:49:44.759 INFO 2106 --- [ost-startStop-1] o.s.web.context.ContextLoader   : Root WebApplicationContext: initialization completed in 1207 ms
2016-07-20 16:49:44.972 INFO 2106 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2016-07-20 16:49:44.977 INFO 2106 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-07-20 16:49:44.978 INFO 2106 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-07-20 16:49:44.978 INFO 2106 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-07-20 16:49:44.978 INFO 2106 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-07-20 16:49:45.184 INFO 2106 --- [   main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@543e710e: startup date [Wed Jul 20 16:49:43 CST 2016]; root of context hierarchy
2016-07-20 16:49:45.251 INFO 2106 --- [   main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users],methods=[GET],consumes=[application/json]}" onto public java.lang.String com.yepit.mapp.rest.UserController.getUser(java.lang.String,java.lang.String)
2016-07-20 16:49:45.253 INFO 2106 --- [   main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-07-20 16:49:45.254 INFO 2106 --- [   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)
2016-07-20 16:49:45.275 INFO 2106 --- [   main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-20 16:49:45.275 INFO 2106 --- [   main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-20 16:49:45.306 INFO 2106 --- [   main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-20 16:49:45.380 INFO 2106 --- [   main] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup
2016-07-20 16:49:45.462 INFO 2106 --- [   main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-07-20 16:49:45.467 INFO 2106 --- [   main] com.yepit.mapp.MappRunApplication  : Started MappRunApplication in 2.573 seconds (JVM running for 3.187)

我們可以看到服務器是Tomcat,端口為8080

6.驗證

推薦大家使用Google的Postman插件來模擬請求

詳解使用Spring Boot開發Restful程序

在發起請求前,請注意需要在Headers中設置Content-Type為application/json

到此一個基本的Restful風格的服務端就已經完成了,全部編碼時間不會超過5分鐘!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/yzllz001/article/details/53504860

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 五月天影院,久久综合, | 国av在线 | 久久一区国产 | 九九热国产视频 | av手机在线免费播放 | 国产日韩成人 | wwwxxx免费视频 | 在线 日本 制服 中文 欧美 | 国产成人精品区一区二区不卡 | 黄色片在线播放 | 黄色片网站在线看 | 九九热视频在线 | 极品xxxx欧美一区二区 | 毛片在线视频观看 | 中文字幕在线观看视频一区 | 男人久久天堂 | 久久久久性 | 九九热精 | 国产精品视频免费在线观看 | 深夜福利久久久 | xxxx hd videos| av电影网站在线 | 国产精品久久久网站 | 欧美视频一二三区 | 操你逼| 国av在线 | 免费视频www在线观看 | 亚洲一区二区三区在线免费观看 | www.99tv| 黄色1级视频 | 日韩一级免费 | 欧美国产日韩在线观看成人 | 黄色成人在线播放 | 欧美性生交xxxxx免费观看 | 成人男女视频 | 国产精品地址 | 美国av免费看 | 成年免费网站 | 国产精品亚洲一区二区三区在线观看 | 亚洲码无人客一区二区三区 | 精品无码一区在线观看 |