GET、POST方式提時, 根據request header Content-Type的值來判斷:
- application/x-www-form-urlencoded, 可選(即非必須,因為這種情況的數據@RequestParam, @ModelAttribute也可以處理,當然@RequestBody也能處理);
- multipart/form-data, 不能處理(即使用@RequestBody不能處理這種格式的數據);
- 其他格式, 必須(其他格式包括application/json, application/xml等。這些格式的數據,必須使用@RequestBody來處理);
代碼:
- 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);
- }
- }