一、創建一個簡單的接口,返回json
1、創建相應的包和類
一般我們會分包進行創建,我這裏簡單創建了一個controller 的包,裏面寫相關的接口controller
然後再test包下我創建了一個Application 類,這個名詞可以自己起。這個類是運行springboot的入口,需要進行配置下。
Application 代碼如下:
2、Application 類代碼講解
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
@SpringBootApplication 註解:
@SpringBootApplication註解一般放在項目的一個啓動類上,用來把啓動類注入到容器中,用來定義容器掃描的範圍,用來加載classpath環境中一些bean
@SpringBootApplication =@Configuration+@EnableAutoConfiguration+@ComponentScan
springboot 把這幾個註解整合了。只需要寫一個就可以
然後在main方法中添加SpringApplication.run(Application.class,args); 來啓動應用程序
3、TestController類講解
package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestController {
//測試用的集合
Map<String,Object> params=new HashMap<>();
/**
* 第一個接口測試
* @param name
* @return
*/
@RequestMapping("/test")
public Object getTest(String name){
params.clear();
params.put("name",name);
return params;
}
}
這裏用到了兩個註解,@RestController、 @RequestMapping
@RestController and @RequestMapping是springMVC的註解,不是springboot特有的
(1) @RestController
@RestController 是spring4 新加入的註解,相當於**@Controller +@ResponseBody** 兩個註解的功能和
@Controller的作用表示該類是一個控制器,可以接受用戶的輸入並調用模型和視圖去完成用戶的需求。控制器本身不輸出也不處理任何東西。 我們寫的接口也就是控制器裏面的方法。
@ResponseBody 表示 請求以json映射的方式返回。
所以@RestController註解的類就可以 接受請求——返回json
(2) @RequestMapping
這個註解可以加載類和方法上,我理解是表示資源的映射吧,爲web請求指明路徑。可以定義不同的映射規則
註解在方法上表示當前方法時一個web請求的處理方法,註解在類上,應該是常用的請求地址或路由啥的
上面方法中在@RequestMapping("/test") 方法中的"/test" 就是外部訪問的接口地址,暫時我們沒有指定請求方法
這樣外部就能通過 http://localhost:8080/test?name=張三 來進行調用這個接口。
二、 get、post請求實戰
1,get請求實戰
package com.test.controller;
import com.test.domain.User;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@RestController
public class GetController {
Map<String, Object> params = new HashMap<>();
/**
* 從路徑中取值
*
* @param cityID
* @param userID
* @return
*/
@RequestMapping(path = "/{city_id}/{user_id}")
public Object getUserID(@PathVariable("city_id") String cityID,
@PathVariable("user_id") String userID) {
params.clear();
params.put("cityID", cityID);
params.put("userID", userID);
return params;
}
/**
* 測試@GetMapping註解,以get方式請求接口
* @param id
* @param name
* @return
*/
@GetMapping(value = "/v1/getUser")
public Object getMappingTest(String id, String name) {
params.clear();
params.put("name", name);
params.put("id", id);
return params;
}
/**
* 測試,設置參數默認值,別名、以及是否必傳參數
* @param id
* @param username
* @return
*/
@RequestMapping("/v1/getUser1")
public Object getMyappingTest1(@RequestParam(defaultValue = "888",name = "uid") String id, @RequestParam(required = true) String username){
params.clear();
params.put("username", username);
params.put("id", id);
return params;
}
/**
* 功能描述:bean對象傳參
* 注意:1、注意需要指定http頭爲 content-type爲application/json
* 2、使用body傳輸數據
* @param user
* @return
*/
@RequestMapping("/v1/save_user")
public Object saveUser(@RequestBody User user){
params.clear();
params.put("user", user);
return params;
}
/**
* 功能描述:測試獲取http頭信息
* @param accessToken
* @param id
* @return
*/
@GetMapping("/v1/get_header")
public Object getHeader(@RequestHeader("access_token") String accessToken, String id){
params.clear();
params.put("access_token", accessToken);
params.put("id", id);
return params;
}
/**
* 以HttpServletRequest獲取所有請求數據
* @param request
* @return
*/
@GetMapping("/v1/test_request")
public Object testRequest(HttpServletRequest request){
params.clear();
String id = request.getParameter("id");
params.put("id",id);
return params;
}
@PostMapping("/v1/login")
public Object login(@RequestParam(required = true) String userName, @RequestParam(required = true)String passWrod){
params.clear();
params.put("name",userName);
params.put("pwd",passWrod);
return params;
}
@PostMapping("/v1/class")
public Object loginTest(User user){
params.clear();
params.put("name",user.getName);
params.put("pwd",user.getPassword);
return params;
}
@PutMapping("/v1/put")
public Object put(String id){
params.clear();
params.put("id", id);
return params;
}
@DeleteMapping("/v1/del")
public Object del(String id){
params.clear();
params.put("id", id);
return params;
}
}
註解簡介: 1、單一參數@RequestMapping(path = "/{id}", method = RequestMethod.GET)
1) public String getUser(@PathVariable String id ) {}
2)@RequestMapping(path = "/{depid}/{userid}", method = RequestMethod.GET) 可以同時指定多個提交方法
getUser(@PathVariable("depid") String departmentID,@PathVariable("userid") String userid)
3)get、post、put、delete四種註解的封裝
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
4)@RequestParam(value = "name", required = true)
可以設置默認值,可以設置別名name="",可以設置是否必傳 requeid=true或false
4)@RequestBody 請求體映射實體類,以json映射javaBean
需要指定http頭爲 content-type爲application/json charset=utf-8
5)@RequestHeader 請求頭,比如鑑權,可以獲取請求頭
@RequestHeader("access_token") String accessToken
6)HttpServletRequest request自動注入獲取參數,可以拿到任何請求數據
2,@RequestParam和@PathVariable的用法與區別
詳細區別
@RequestParam和@PathVariable
相同點與區別
@RequestParam和@PathVariable都能夠完成類似的功能——因爲本質上,它們都是用戶的輸入,只不過輸入的部分不同,一個在URL路徑部分,另一個在參數部分。要訪問一篇博客文章,這兩種URL設計都是可以的:
通過@PathVariable,例如/blogs/1
通過@RequestParam,例如blogs?blogId=1
那麼究竟應該選擇哪一種呢?建議:
1、當URL指向的是某一具體業務資源(或資源列表),例如博客,用戶時,使用@PathVariable
2、當URL需要對資源或者資源列表進行過濾,篩選時,用@RequestParam
例如我們會這樣設計URL:
/blogs/{blogId}
/blogs?state=publish而不是/blogs/state/publish來表示處於發佈狀態的博客文章
更多用法
一旦我們在方法中定義了@RequestParam變量,如果訪問的URL中不帶有相應的參數,就會拋出異常——這是顯然的,Spring嘗試幫我們進行綁定,然而沒有成功。但有的時候,參數確實不一定永遠都存在,這時我們可以通過定義required屬性:
@RequestParam(value = “id”, required = false)
當然,在參數不存在的情況下,可能希望變量有一個默認值:
@RequestParam(value = “id”, required = false, defaultValue = “0”)
三,常用註解
@Data,
@Data : 註解在類上, 爲類提供讀寫屬性, 此外還提供了 equals()、hashCode()、toString() 方法
@Data
public class ActivityListParam{
private Integer id;
private Integer page;
private Integer count;
...
}
以往我的做法,還會通過快捷方法生成Getter,Setter,equals,hashCode,toString方法。但這個類裏只聲明瞭變量,沒有各種方法,而在類名上加@Data註解,導入依賴:lombok.Data。在另一個類中導入該入參類後,通過activityListParam.是可以點出沒有寫的Get,Set等方法。因此,我理解爲:@Data註解在類上時,簡化java代碼編寫,爲該類提供讀寫屬性,還提供了equals(),hashCode(),toString()方法
@Getter/@Setter : 註解在類上, 爲類提供讀寫屬性
@Configuration和@Bean
注意:@Configuration註解的配置類有如下要求:
@Configuration不可以是final類型;
@Configuration不可以是匿名類;
嵌套的configuration必須是靜態類。
@Configuration 和 @Bean 註解
帶有 @Configuration 的註解類表示這個類可以使用 Spring IoC 容器作爲 bean 定義的來源。@Bean 註解告訴 Spring,一個帶有 @Bean 的註解方法將返回一個對象,該對象應該被註冊爲在 Spring 應用程序上下文中的 bean。
例子如下:
HelloWorld.java
package com.how2java.w3cschool.baseonjava;
public class HelloWorld {
private String message;
public void getMessage() {
System.out.println("Your message is:" + message);
}
public void setMessage(String message) {
this.message = message;
}
}
HelloWorldConfig.java
package com.how2java.w3cschool.baseonjava;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* 帶有 @Configuration 的註解類表示這個類可以使用 Spring IoC 容器作爲 bean 定義的來源。
* @Bean 註解告訴 Spring,一個帶有 @Bean 的註解方法將返回一個對象,該對象應該被註冊爲在 Spring 應用程序上下文中的 bean。
*/
@Configuration
public class HelloWorldConfig {
// 帶有 @Bean 註解的方法名稱作爲 bean 的 ID,它創建並返回實際的 bean。也就是此時的bean
// id爲helloWorld,你的配置類可以聲明多個 @Bean。
// 一旦定義了配置類,你就可以使用 AnnotationConfigApplicationContext 來加載並把他們提供給 Spring 容器
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
MainApp.java
package com.how2java.w3cschool.baseonjava;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
如果在HelloWorldConfig.java中忘了@Bean的註解,將會遇到以下的錯誤
“No unique bean of type [com.how2java.w3cschool.baseonjava.HelloWorld] is defined: expected single bean but found 0: ”
@Configuration和@Bean,@ComponentScan和@Component四個之間的關聯
從Spring3.0,@Configuration用於定義配置類,可替換xml配置文件,被註解的類內部包含有一個或多個被@Bean註解的方法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器。
@Configuation等價於
@Bean 等價於
@ComponentScan等價於<context:component-scan base-package=“com.dxz.demo”/>
@Component 等價於
前言
@Configuration 用於定義配置類,可替換XML配置文件,被註解的類內部包含一個或多個@Bean註解方法。可以被AnnotationConfigApplicationContext或者AnnotationConfigWebApplicationContext 進行掃描。用於構建bean定義以及初始化Spring容器。
實例
@Configuration 加載Spring方法
Car.java
public class Car {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
定義Config類
@Configuration
public class Config {
public Config() {
System.out.println("TestConfig容器初始化...");
}
@Bean(name = "getMyCar")
public Car getCar() {
Car c = new Car();
c.setName("dankun");
return c;
}
}
實例化
public void testConfig() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Car car = (Car)context.getBean("car");
System.out.println(car.getName());
}
// 輸出
// TestConfig容器初始化...
// dankun
@Configuration + @Component
@Configuration也附帶了@Component的功能。所以理論上也可以使用@Autowared功能。上述代碼可以改成下面形式
Car.java
@Component
public class Car {
@Value("dankun")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Config.java
@Configuration
@ComponentScan("com.wuyue.annotation")
public class Config {
public Config() {
System.out.println("TestConfig容器初始化...");
}
測試主入口
public class TestConfig {
@Test
public void testConfig() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Car car = (Car)context.getBean("car");
System.out.println(car.getName());
}
}
// 輸出
// TestConfig容器初始化...
// dankun
總結
@Configuation等價於
@Bean 等價於
@ComponentScan等價於<context:component-scan base-package=“com.dxz.demo”/>
@Component 等價於
@Bean VS @Component
兩個註解的結果是相同的,bean都會被添加到Spring上下文中。
@Component 標註的是類,允許通過自動掃描發現。@Bean需要在配置類@Configuation中使用。
@Component類使用的方法或字段時不會使用CGLIB增強。而在@Configuration類中使用方法或字段時則使用CGLIB創造協作對象
假設我們需要將一些第三方的庫組件裝配到應用中或者 我們有一個在多個應用程序中共享的模塊,它包含一些服務。並非所有應用都需要它們。
如果在這些服務類上使用@Component並在應用程序中使用組件掃描,我們最終可能會檢測到超過必要的bean。導致應用程序無法啓動
但是我們可以使用 @Bean來加載
因此,基本上,使用@Bean將第三方類添加到上下文中。和@Component,如果它只在你的單個應用程序中
@EnableSwagger2,
@Api:用在請求的類上,表示對類的說明
tags=“說明該類的作用,可以在UI界面上看到的註解”
value=“該參數沒什麼意義,在UI界面上也看到,所以不需要配置”
@ApiOperation:用在請求的方法上,說明方法的用途、作用
value=“說明方法的用途、作用”
notes=“方法的備註說明”
@Controller、@RestController註解區別
@Controller
方法的返回值。默認是跳轉路徑。
如果想返回json對象,必須在方法的上面加@ResponseBody
@RestController
方法返回值,默認是json對象,也就是相當於@Controller裏面的方法上添加了@ResponseBody如果方法返回值,需要跳轉,那麼方法的返回類型必須是View 或者ModelAndView.
一,@Controller
只是定義了一個控制器類,而使用 @RequestMapping 註解的方法纔是處理請求的處理器。
@Controller
public class HospitalController {
//注入Service服務對象
@Autowired
private HospitalService hospitalService;
@RequestMapping(method = RequestMethod.POST, value = "/findAllHospital")
@ResponseBody
public Map findAllHospital(final HttpServletRequest request ,
@RequestBody Map parmMap , HttpServletResponse response){
Map map=parmMap ;//@RequestBody註解原因,系統會自動把request數據變成parmMap
........
........
return map;
}
}
用@Controller定義一個控制器類,
用@RequestMapping給出外界訪問方法的路徑,或者說觸發路徑 ,觸發條件。
用@ResponseBody標記Controller類中的方法。把return的結果變成JSON對象返回。(如果沒有這個註解,這個方法只能返回要跳轉的路徑即跳轉的html/JSP頁面。有這個註解,可以不跳轉頁面,只返回JSON數據)
二、@RestController註解
@RestController 也是Spring框架提供的註解。(Spring4.0之後新增的)
@RestController 註解相當於 @Controller + @ResponseBody 合在一起的作用。
Controller類中的方法返回值,默認是json對象,也就是相當於@Controller裏面的方法上添加了@ResponseBody
如果方法返回值,需要跳轉,那麼方法的返回類型必須是View 或者ModelAndView.
@RestController
public class HospitalController {
//注入Service服務對象
@Autowired
private HospitalService hospitalService;
@RequestMapping(method = RequestMethod.POST, value = "/findAllHospital")
//方法上面可以不需要@ResponseBody註解,因爲類上面用的是@RestController註解
public Map findAllHospital(final HttpServletRequest request ,
@RequestBody Map parmMap , HttpServletResponse response){
Map map=parmMap ;//@RequestBody註解原因,系統會自動把request數據變成parmMap
........
........
return map;
}
}
swagger2的使用 @Api,@ApiOperation等
最常用的5個註解
@Api:修飾整個類,描述Controller的作用
@ApiOperation:描述一個類的一個方法,或者說一個接口
@ApiParam:單個參數描述
@ApiModel:用對象來接收參數
@ApiProperty:用對象接收參數時,描述對象的一個字段
其它若干
@ApiResponse:HTTP響應其中1個描述
@ApiResponses:HTTP響應整體描述
@ApiClass
@ApiError
@ApiErrors
@ApiParamImplicit
@ApiParamsImplicit
4.1、@Api修飾整個類,描述Controller的作用
4.2、@ApiOperation
用於描述一個方法或者接口
可以添加的參數形式:@ApiOperation(value = “接口說明”, httpMethod = “接口請求方式”, response = “接口返回參數類型”, notes = “接口發佈說明”)
4.3、@ApiParam單個參數描述
@ApiParam(required = “是否必須參數”, name = “參數名稱”, value = “參數具體描述”,dateType="變量類型”,paramType="請求方式”)
4.4、@ApiImplicitParam 一個請求參數
@ApiImplicitParam(required = “是否必須參數”, name = “參數名稱”, value = “參數具體描述”,dateType="變量類型”,paramType="請求方式”)
@ApiOperation(value = "根據用戶名獲取用戶的信息",notes = "查詢數據庫中的記錄",httpMethod = "POST",response = String.class)
@ApiImplicitParam(name = "userName",value = "用戶名",required = true,dataType = "String",paramType = "query")
4.5、@ApiImplicitParams 多個請求參數
參數和@ApiImplicitParam一致,只是這個註解可以添加多個參數而已
@ApiImplicitParams({
@ApiImplicitParam(name = "nickName",value = "用戶的暱稱",paramType = "query",dataType = "String",required = true),
@ApiImplicitParam(name = "id",value = "用戶的ID",paramType = "query",dataType = "Integer",required = true)
})
public String getUserInfoByNickName(String nickName, Integer id) {
return "1234";
}
其餘的都類似。
整個controller的代碼如下
@RestController
@RequestMapping("/swagger")
@Api(value = "swagger2的demo例子")
public class SwaggerController {
@RequestMapping("/swagger")
@ResponseBody
@ApiOperation(value = "根據用戶名獲取用戶的信息",notes = "查詢數據庫中的記錄",httpMethod = "POST",response = String.class)
@ApiImplicitParam(name = "userName",value = "用戶名",required = true,dataType = "String",paramType = "query")
public String getUserInfo(String userName) {
return "1234";
}
@RequestMapping(value = "/getuserinfobynickname",method = {RequestMethod.GET,RequestMethod.POST})
@ResponseBody
@ApiOperation(value = "根據用戶暱稱獲取用戶信息",notes = "查詢數據庫中的記錄")
@ApiImplicitParams({
@ApiImplicitParam(name = "nickName",value = "用戶的暱稱",paramType = "query",dataType = "String",required = true),
@ApiImplicitParam(name = "id",value = "用戶的ID",paramType = "query",dataType = "Integer",required = true)
})
public String getUserInfoByNickName(String nickName, Integer id) {
return "1234";
}
@RequestMapping(value = "/getuserinfobyid",method = {RequestMethod.GET,RequestMethod.POST})
@ResponseBody
@ApiOperation(value = "根據用戶id獲取用戶信息",notes = "查詢數據庫中的記錄",httpMethod = "POST")
public String getUserInfoById(@ApiParam(name = "nickName",value = "用戶的暱稱",required = true,defaultValue = "123-默認值")
String nickName,@ApiParam(name = "id",value = "用戶ID",required = true) Integer id) {
return "1234";
}
@RequestMapping(value = "/userregister",method = {RequestMethod.GET,RequestMethod.POST})
@ResponseBody
@ApiOperation(value = "register",notes = "註冊的實體類")
public Register userRegister(Register register) {
return register;
}
}
對應的實體說明
@ApiModel(value = "用戶註冊的實體")
public class Register {
@ApiModelProperty(name = "userName",notes = "用戶名",dataType = "String",required = true)
private String userName;
@ApiModelProperty(name = "nickName",notes = "用戶暱稱",dataType = "String",required = true)
private String nickName;
@ApiModelProperty(name = "age",notes = "用戶年齡",dataType = "int",required = true)
private int age;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Register{" +
"userName='" + userName + '\'' +
", nickName='" + nickName + '\'' +
", age=" + age +
'}';
}
}
@EnableAutoConfiguration,
@EnableAutoConfiguration:能夠自動配置spring的上下文,試圖猜測和配置你想要的bean類,通常會自動根據你的類路徑和你的bean定義自動配置
@ResponseBody,
1 @Controller
2 @RequestMapping("/")
3 public class HelloController {
4 @RequestMapping(value = "/helloWorld", method = RequestMethod.GET)
5 @ResponseBody
6 public String helloWorld() {
7 return"Hello World";
8 }
9 }
10
11 運行以上代碼,在瀏覽器地址欄輸入: http://localhost:8080/helloWorld
12 運行結果,頁面上輸出 Hello World
如果不加@responseBody註解,運行結果會怎樣?
結果表明:如果在一個方法上使用了@RequestMapping註解,這時候,方法的返回值通常解析爲跳轉的路徑, 也就是說,要跳轉到指定的jsp頁面。在這個代碼實例中,要跳轉到的是 Hello World.jsp 頁面。 因爲工程中尚未添加這個jsp文件,所以報出了 404 錯誤 (The requested resource is not available)。
如果添加了 @ResponseBody 這個註解, 則表明該方法的返回值直接寫入到 HTTP Response Body 中。 這就是說,如果返回的是JSON, 就得必須添加 @ResponseBody 這個註解
一般在異步獲取數據時使用,在使用@RequestMapping後,返回值通常解析爲跳轉路徑,加上@responsebody後返回結果不會被解析爲跳轉路徑,而是直接寫入HTTP response body中。比如異步獲取json數據,加上@responsebody後,會直接返回json數據。
@SpringBootApplication,
之前用戶使用的是3個註解註解他們的main類。分別是@Configuration,@EnableAutoConfiguration,@ComponentScan。由於這些註解一般都是一起使用,spring boot提供了一個統一的註解@SpringBootApplication。
@SpringBootApplication = (默認屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
@SpringBootApplication
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
分開解釋@Configuration,@EnableAutoConfiguration,@ComponentScan。
1、@Configuration:提到@Configuration就要提到他的搭檔@Bean。使用這兩個註解就可以創建一個簡單的spring配置類,可以用來替代相應的xml配置文件。
<beans>
<bean id = "car" class="com.test.Car">
<property name="wheel" ref = "wheel"></property>
</bean>
<bean id = "wheel" class="com.test.Wheel"></bean>
相當於:
@Configuration
public class Conf {
@Bean
public Car car() {
Car car = new Car();
car.setWheel(wheel());
return car;
}
@Bean
public Wheel wheel() {
return new Wheel();
}
}
@Configuration的註解類標識這個類可以使用Spring IoC容器作爲bean定義的來源。@Bean註解告訴Spring,一個帶有@Bean的註解方法將返回一個對象,該對象應該被註冊爲在Spring應用程序上下文中的bean。
2、@EnableAutoConfiguration:能夠自動配置spring的上下文,試圖猜測和配置你想要的bean類,通常會自動根據你的類路徑和你的bean定義自動配置。
3、@ComponentScan:會自動掃描指定包下的全部標有@Component的類,並註冊成bean,當然包括@Component下的子註解@Service,@Repository,@Controller。