springmvc传值的⼏种⽅式及使⽤postman测试1、单参数,使⽤@RequestParam注解,且返回json数据
@RequestMapping(value ="/testRequestOneParam")
@ResponseBody
public String  testRequestOneParam(@RequestParam String name)
{
ReturnTrafficData r = new ReturnTrafficData();
r.setCode(0);
r.setData("我是Data");
r.setMessage("我是通⾏证信息");
JSONString(r);
}
测试请求路径:
2、多参数,使⽤@RequestParam注解 ,且返回json数据
@RequestMapping(value ="/testRequestTwoParam")
@ResponseBody
public String  testRequestTwoParam(@RequestParam String name,@RequestParam String sex)
{
ReturnTrafficData r = new ReturnTrafficData();
r.setCode(0);
r.setData("我是Data");
r.setMessage("我是通⾏证信息");
JSONString(r);
}
测试请求路径:
3、单POJO参数,使⽤@RequestBody注解 ,且返回json数据
@RequestMapping(value ="/saveTrafficPermit")//,method = RequestMethod.GET
public @ResponseBody String saveTrafficPermit(@RequestBody TrafficPermitData trafficPermitData) throws IOException
{
System.out.Clientid());
System.out.Data());
System.out.Sign());
System.out.Timestamp());
ReturnTrafficData r = new ReturnTrafficData();
r.setCode(0);
r.setData("我是Data");
r.setMessage("我是通⾏证信息");
JSONString(r);
}
测试请求路径:
4、多参数,使⽤@RequestBody注解和@RequestParam注解 ,且返回json数据
@RequestMapping(value ="/testRequestBodyAndRequestParam",method = RequestMethod.POST)
@ResponseBody
public String  testRequestBodyAndRequestParam(@RequestBody TrafficPermitData trafficPermitData,@RequestParam String name) {
System.out.Clientid());
postman的中文翻译System.out.Data());
System.out.Sign());
System.out.Timestamp());
System.out.println(name);
ReturnTrafficData r = new ReturnTrafficData();
r.setCode(0);
r.setData("我是Data");
r.setMessage("我是通⾏证信息");
JSONString(r);
}
测试请求路径:
5、单参数,使⽤@PathVariable注解 ,将参数当做请求路径,且返回json数据
@RequestMapping(value ="/testPathVariableOne/{name}")
@ResponseBody
public String  testPathVariableOne(@PathVariable("name") String name)
{
System.out.println(name);
ReturnTrafficData r = new ReturnTrafficData();
r.setCode(0);
r.setData("我是Data");
r.setMessage("我是通⾏证信息");
JSONString(r);
}
测试请求路径,由于postman的 url地址不能包含中⽂,所以在发送请求之前,需要对中⽂进⾏encodeURI转换
5、多参数,使⽤@PathVariable注解 ,将参数当做请求路径,且返回json数据
@RequestMapping(value ="/testPathVariableTwo/{name}/{sex}")
@ResponseBody
public String  testPathVariableTwo(@PathVariable("name") String name,@PathVariable String sex) {
System.out.println(name);
System.out.println(sex);
ReturnTrafficData r = new ReturnTrafficData();
r.setCode(0);
r.setData("我是Data");
r.setMessage("我是通⾏证信息");
JSONString(r);
}
测试请求路径如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。