spring boot get和post請求,以及requestbody為json串時候的處理
阿新 • • 發佈:2019-01-09
package com.example.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; 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; import com.example.bean.RequestLoginBean; import com.example.response.BaseResponse; import com.google.gson.Gson; @RestController @RequestMapping(value = "/index") public class Login { /** * index home * * @return */ @RequestMapping(value = "/home") public String home() { return "index home"; } /** * 得到1個引數 * * @param name * 使用者名稱 * @return 返回結果 */ @GetMapping(value = "/{name}") public String index(@PathVariable String name) { return "oh you are " + name + "<br> nice to meet you";// \n不起作用了,那就直接用html中的標籤吧 } /** * 簡單post請求 * * @param name * @param pwd * @return */ @RequestMapping(value = "/testpost", method = RequestMethod.POST) public String testpost() { System.out.println("hello test post"); return "ok"; } /** * 同時得到兩個引數 * * @param name * 使用者名稱 * @param pwd * 密碼 * @return 返回結果 */ @GetMapping(value = "/login/{name}&{pwd}") public String login(@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請求去登陸 * * @param name * @param pwd * @return */ @RequestMapping(value = "/loginbyget", method = RequestMethod.GET) public String loginByGet(@RequestParam(value = "name", required = true) String name, @RequestParam(value = "pwd", required = true) String pwd) { return login4Return(name, pwd); } /** * 通過post請求去登陸 * * @param name * @param pwd * @return */ @RequestMapping(value = "/loginbypost", method = RequestMethod.POST) public String loginByPost(@RequestParam(value = "name", required = true) String name, @RequestParam(value = "pwd", required = true) String pwd) { System.out.println("hello post"); return login4Return(name, pwd); } /** * 引數為一個bean物件.spring會自動為我們關聯對映 * @param loginBean * @return */ @RequestMapping(value = "/loginbypost1", method = { RequestMethod.POST, RequestMethod.GET }) public String loginByPost1(RequestLoginBean loginBean) { if (null != loginBean) { return login4Return(loginBean.getName(), loginBean.getPwd()); } else { return "error"; } } /** * 請求內容是一個json串,spring會自動把他和我們的引數bean對應起來,不過要加@RequestBody註解 * * @param name * @param pwd * @return */ @RequestMapping(value = "/loginbypost2", method = { RequestMethod.POST, RequestMethod.GET }) public String loginByPost2(@RequestBody RequestLoginBean loginBean) { if (null != loginBean) { return login4Return(loginBean.getName(), loginBean.getPwd()); } else { return "error"; } } /** * 對登入做出響應處理的方法 * * @param name * 使用者名稱 * @param pwd * 密碼 * @return 返回處理結果 */ private String login4Return(String name, String pwd) { String result; BaseResponse response = new BaseResponse(); if (name.equals("admin") && pwd.equals("admin")) { result = "hello welcome admin"; response.setState(true); } else { result = "oh sorry user name or password is wrong"; response.setState(false); } System.out.println("收到請求,請求結果:" + result); return new Gson().toJson(response); } }