@RestController
@RequestMapping("/test")
public class TestController {
// application/x-www-form-urlencoded
// 可選(即非必須,因爲這種情況的數據@RequestParam, @ModelAttribute也可以處理,當然@RequestBody也能處理);
//multipart/form-data
// 不能處理(即使用@RequestBody不能處理這種格式的數據);
//其他格式
// 必須(其他格式包括application/json, application/xml等。這些格式的數據,必須使用@RequestBody來處理);
/**
* 普通請求
*
* http://localhost:8089/test/test1
*/
@RequestMapping(value = "/test1")
public String test1() {
return "test1";
}
/**
* 簡單get請求
* 組合註解 @GetMapping
* 是@RequestMapping(method = RequestMethod.GET)的縮寫
*
* http://localhost:8089/test/test2/sun
*/
@GetMapping(value = "test2/{name}")
public String test2(@PathVariable String name) {
// \n不起作用了,那就直接用html中的標籤吧
return "oh you are " + name + "<br> nice to meet you";
}
/**
* 簡單get請求2
* 註解 @PathVariable 映射URL綁定的佔位符
* 1)帶佔位符的URL是Spring3.0新增的功能,該功能在SpringMVC向REST目標挺進發展過程中具有里程碑的意義。
* 2)通過@PathVariable可以將URL中佔位符參數綁定到控制器處理方法的入參中:
* URL中的{xxx}佔位符可以通過@PathVariable("xxx")綁定到操作方法的入參中
*
* http://localhost:8089/test/test3/admin&admin
*/
@GetMapping(value = "/test3/{name}&{pwd}")
public String test3(@PathVariable String name, @PathVariable String pwd) {
if (name.equals("admin") && pwd.equals("admin")) {
return "hello welcome admin";
} else {
return "oh sorry user name or password is wrong";
}
}
/**
* get請求
* 註解 @RequestParam綁定請求參數值
* 在處理方法入參使用@RequestParam可以把請求參數傳遞給請求方法,@RequestParam包含的屬性值:
* --- value :參數名稱
* --- required :是否必須,默認爲true,表示請求參數中必須包含對應的參數,否則拋出異常。
* --- defaultValue:當請求參數缺少或者有請求參數但值爲空時,值採用該設置值
*
* http://localhost:8089/test/test4?name=111&pwd=111
*/
@RequestMapping(value = "/test4", method = RequestMethod.GET)
public String test4(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "pwd", required = true) String pwd) {
if (name.equals("admin") && pwd.equals("admin")) {
return JSON.toJSONString(Result.success());
} else {
return JSON.toJSONString(Result.error());
}
}
/**
* 簡單post請求
* 組合註解 @PostMapping
* 是@RequestMapping(method = RequestMethod.POST)的縮寫。
*
* http://localhost:8089/test/test5
*/
@RequestMapping(value = "/test5", method = RequestMethod.POST)
public String test5() {
System.out.println("hello test5");
return "test5";
}
/**
* post請求
*
* http://localhost:8089/test/test6?name=111&pwd=111
*/
@RequestMapping(value = "/test6", method = RequestMethod.POST)
public String test6(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "pwd", required = true) String pwd) {
if (name.equals("admin") && pwd.equals("admin")) {
return JSON.toJSONString(Result.success());
} else {
return JSON.toJSONString(Result.error());
}
}
/**
* 參數爲一個bean對象.spring會自動爲我們關聯映射
*
* get post
* http://localhost:8089/test/test7?id=1
*
* post form-data和 x-www-form-urlencoded
* http://localhost:8089/test/test7
* id 1
*/
@RequestMapping(value = "/test7", method = { RequestMethod.POST, RequestMethod.GET })
public String test7(BaseEntity entity) {
if (null != entity && entity.getId() == 1) {
return JSON.toJSONString(Result.success());
} else {
return JSON.toJSONString(Result.error());
}
}
/**
* 請求內容是一個json串,spring會自動把他和我們的參數bean對應起來
* 不過要加@RequestBody註解
*
* post row(application/json)
* http://localhost:8089/test/test8
* id 1
*/
@RequestMapping(value = "/test8", method = { RequestMethod.POST, RequestMethod.GET })
public String test8(@RequestBody BaseEntity entity) {
if (null != entity && entity.getId() == 1) {
return JSON.toJSONString(Result.success());
} else {
return JSON.toJSONString(Result.error());
}
}
}
SpringMVC前後端接口使用規範
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.