ati htm ack enc clip multipart response line 作用
GET,POST 方式提时, 根据 request header Content-Type 的值来判断:
application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据 @RequestParam, @ModelAttribute 也可以处理,当然 @RequestBody 也能处理);
multipart/form-data, 不能处理(即使用 @RequestBody 不能处理这种格式的数据);
其他格式, 必须(其他格式包括 application/json, application/xml 等.这些格式的数据,必须使用 @RequestBody 来处理);
代码:
spring boot get 和 post 请求, 以及 requestbody 为 json 串时候的处理
[java] view plain copy 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") publicclassLogin {
/**
* index home
*
* @return
*/
@RequestMapping(value = "/home") publicString home() {
return"index home";
}
/**
* 得到 1 个参数
*
* @param name
* 用户名
* @return 返回结果
*/
@GetMapping(value = "/{name}") publicString index(@PathVariableString name) {
return"oh you are" + name + "
nice to meet you"; // \n 不起作用了, 那就直接用 html 中的标签吧
}
/**
* 简单 post 请求
*
* @param name
* @param pwd
* @return
*/
@RequestMapping(value = "/testpost", method = RequestMethod.POST) publicString testpost() { System.out.println("hello test post");
return"ok";
}
/**
* 同时得到两个参数
*
* @param name
* 用户名
* @param pwd
* 密码
* @return 返回结果
*/
@GetMapping(value = "/login/{name}&{pwd}") publicString login(@PathVariableString name, @PathVariableString 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) publicString 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) publicString 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
}) publicString 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
}) publicString loginByPost2(@RequestBodyRequestLoginBean loginBean) {
if (null != loginBean) {
return login4Return(loginBean.getName(), loginBean.getPwd());
}
else {
return"error";
}
}
/**
* 对登录做出响应处理的方法
*
* @param name
* 用户名
* @param pwd
* 密码
* @return 返回处理结果
*/
privateString login4Return(String name, String pwd) { String result; BaseResponse response = newBaseResponse();
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);
returnnewGson().toJson(response);
}
}
来源: http://www.bubuko.com/infodetail-2463451.html