spring boot getとpost要求、及びrequest bodyがjsonの時の処理です.
4299 ワード
GET、POST方式で提する時、request header Conteen-Typeの値によって判断します. appication/x-wn-form-urlencoded、オプション(必須ではないので、この場合のデータ@Request Param、@ModelAttributeも処理できます.もちろん@Request Bodyも処理できます.) multiipad/form-dataでは処理できません. 他のフォーマットは必須です.(他のフォーマットにはaplication/json、appication/xmlなどがあります.これらのフォーマットのデータは@Request Bodyを使って処理しなければなりません.) コード:
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 + "
nice to meet you";//
, 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);
}
}