欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解使用Spring Boot開(kāi)發(fā)Restful程序

 更新時(shí)間:2017年05月24日 14:56:47   作者:yzllz001  
本篇文章主要介紹了詳解使用Spring Boot開(kāi)發(fā)Restful程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、簡(jiǎn)介

Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開(kāi)發(fā)人員不再需要定義樣板化的配置。通過(guò)這種方式,Boot致力于在蓬勃發(fā)展的快速應(yīng)用開(kāi)發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。

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

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

  1. 為所有的Spring開(kāi)發(fā)工作提供一個(gè)更快、更廣泛的入門經(jīng)驗(yàn)。
  2. 開(kāi)箱即用,你也可以通過(guò)修改默認(rèn)值來(lái)快速滿足你的項(xiàng)目的需求。
  3. 提供了一系列大型項(xiàng)目中常見(jiàn)的非功能性特性,如嵌入式服務(wù)器、安全、指標(biāo),健康檢測(cè)、外部配置等。

Spring Boot官網(wǎng): http://projects.spring.io/spring-boot/

二、開(kāi)發(fā)環(huán)境準(zhǔn)備

IDE:IntelliJ IDEA

官網(wǎng)地址:https://www.jetbrains.com/idea/download/

JDK:1.8

Maven

數(shù)據(jù)庫(kù):MySQL

我將以一個(gè)用戶積分系統(tǒng)為例,開(kāi)發(fā)一個(gè)Restful風(fēng)格的服務(wù)端

三、第一個(gè)Restful程序

1.新建一個(gè)普通Maven工程

創(chuàng)建項(xiàng)目完成后目錄結(jié)構(gòu)如下圖所示

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

<?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.新建一個(gè)RestController來(lái)接收客戶端的請(qǐng)求,我們來(lái)模擬一個(gè)登錄請(qǐng)求

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. 關(guān)鍵字@RestController代表這個(gè)類是用Restful風(fēng)格來(lái)訪問(wèn)的,如果是普通的WEB頁(yè)面訪問(wèn)跳轉(zhuǎn)時(shí),我們通常會(huì)使用@Controller
  2. value = "/users/{username}" 代表訪問(wèn)的URL是"http://host:PORT/users/實(shí)際的用戶名"
  3. method = RequestMethod.GET 代表這個(gè)HTTP請(qǐng)求必須是以GET方式訪問(wèn)
  4. consumes="application/json" 代表數(shù)據(jù)傳輸格式是json
  5. @PathVariable將某個(gè)動(dòng)態(tài)參數(shù)放到URL請(qǐng)求路徑中
  6. @RequestParam指定了請(qǐng)求參數(shù)名稱

4.新建啟動(dòng)Restful服務(wù)端的啟動(dòng)類

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.執(zhí)行MappRunApplication的Main方法啟動(dòng)Restful服務(wù),可以看到控制臺(tái)有如下輸出

 . ____   _   __ _ _
 /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
 \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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)


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

6.驗(yàn)證

推薦大家使用Google的Postman插件來(lái)模擬請(qǐng)求

在發(fā)起請(qǐng)求前,請(qǐng)注意需要在Headers中設(shè)置Content-Type為application/json

到此一個(gè)基本的Restful風(fēng)格的服務(wù)端就已經(jīng)完成了,全部編碼時(shí)間不會(huì)超過(guò)5分鐘!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring在SingleTon模式下的線程安全詳解

    Spring在SingleTon模式下的線程安全詳解

    這篇文章主要介紹了Spring在SingleTon模式下的線程安全詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java統(tǒng)計(jì)一個(gè)字符串在另外一個(gè)字符串出現(xiàn)次數(shù)的方法

    Java統(tǒng)計(jì)一個(gè)字符串在另外一個(gè)字符串出現(xiàn)次數(shù)的方法

    這篇文章主要介紹了Java統(tǒng)計(jì)一個(gè)字符串在另外一個(gè)字符串出現(xiàn)次數(shù)的方法,涉及java字符串遍歷、正則匹配等相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Springboot異步事件配置和使用示例詳解

    Springboot異步事件配置和使用示例詳解

    Spring框架提供了一套事件處理機(jī)制,允許應(yīng)用在各個(gè)組件之間傳遞狀態(tài)信息,自定義事件通常繼承自ApplicationEvent類,Springboot通過(guò)自動(dòng)配置簡(jiǎn)化了異步處理的配置,實(shí)現(xiàn)開(kāi)箱即用,Spring事件模型核心是觀察者模式,適用于解耦和提高響應(yīng)速度
    2024-10-10
  • java按鈕控件數(shù)組實(shí)現(xiàn)計(jì)算器界面示例分享

    java按鈕控件數(shù)組實(shí)現(xiàn)計(jì)算器界面示例分享

    本文主要介紹了JAVA通過(guò)按鈕數(shù)組來(lái)管理界面中的所有按鈕控件,從而使用最少的代碼實(shí)現(xiàn)模擬的計(jì)算器界面
    2014-02-02
  • Springboot使用jsp具體案例解析

    Springboot使用jsp具體案例解析

    這篇文章主要介紹了Springboot使用jsp具體案例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot YAML語(yǔ)法基礎(chǔ)詳細(xì)整理

    SpringBoot YAML語(yǔ)法基礎(chǔ)詳細(xì)整理

    YAML 是 “YAML Ain’t Markup Language”(YAML 不是一種標(biāo)記語(yǔ)言)的遞歸縮寫。在開(kāi)發(fā)的這種語(yǔ)言時(shí),YAML 的意思其實(shí)是:“Yet Another Markup Language”(仍是一種標(biāo)記語(yǔ)言),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼

    SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼

    這篇文章主要介紹了springboot如何實(shí)現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請(qǐng)求中某個(gè)接口需要升級(jí),正常做法是升級(jí)我們的版本,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-03-03
  • Spring實(shí)戰(zhàn)之容器后處理器操作示例

    Spring實(shí)戰(zhàn)之容器后處理器操作示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之容器后處理器操作,結(jié)合實(shí)例形式分析了spring容器后處理器配置、使用操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2019-12-12
  • Java Iterator迭代器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java Iterator迭代器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    迭代器是一種模式,它可以使得對(duì)于序列類型的數(shù)據(jù)結(jié)構(gòu)的遍歷行為與被遍歷的對(duì)象分離,接下來(lái)通過(guò)本文給大家分享Java Iterator迭代器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友參考下吧
    2017-05-05
  • Java中異常傳播的實(shí)現(xiàn)

    Java中異常傳播的實(shí)現(xiàn)

    在Java中,異常傳播是一個(gè)重要的概念,本文主要介紹了Java中異常傳播的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01

最新評(píng)論