PostMappering中consumes与produces属性的作用

 

哈喽大家好今天跟大家简单聊一聊PostMappering中consumers与produces两个属性的作用

在对接接口中,对方API要求,请求头 HTTP Header 中设置 Content-Type 为 application/x-www-form-urlencoded,响应头 HTTP Header 中 Content-Type 为 application/json。也就是说一个接口中,接收参数方式是application/x-www-form-urlencoded,但是响应回去的方式又是JSON

过程中我犯了傻,请求是form表单形式,我还有requestBody注解的方式接收参数,

form表单提交接收参数的几种方式:

1.request.getParameter("参数名");
@PostMapping(value ="/testT")
public void test(HttpServletRequest request) throws Exception {
String name=request.getParameter("name");
String age=request.getParameter("age");
System.out.println("请求到的参数:name="+name+",age="+age);
}
请求到的参数:name=Angle,age=24

2.@RequestParam(value="参数名")
一个一个参数接收,如果参数比较多,不好用
@PostMapping(value ="/testT")
public void test(@RequestParam(value="name") String name,@RequestParam(value="age") Integer age) throws Exception {
System.out.println("请求到的参数:name="+name+",age="+age);
}
请求到的参数:name=Angle,age=24

3.@ModelAttribute()
自定义实体接收
@PostMapping(value ="/test")
public void test(@ModelAttribute() TestRequest testRequest) throws Exception {
System.out.println("接收到的参数:"+JSON.toJSONString(testRequest));
}

接收到的参数:{"age":"24","name":"Angle"}

4.request.getParameterMap();
将获取参数map
@PostMapping(value ="/testT")
public void test(HttpServletRequest request) throws Exception {
Map<String, String> paramMap = Reflections.transToStringMAP(request.getParameterMap());//获取请求参数并转为Map对象
ObjectMapper objectMapper=new ObjectMapper();
TestRequest testRequest=objectMapper.readValue(JSONObject.toJSONString(paramMap), TestRequest.class);
使用ObjectMapper将map转为实体
System.out.println("请求到的参数:"+JSON.toJSONString(paramMap));
System.out.println(testRequest);
}
请求到的参数:{"name":"Angle","age":"24"}
TestRequest [name=Angle, age=24]

配合使用也可以实现@ModelAttribute()注解方式接收参数,建议使用注解方式获取参数,简单明了
回归正题下面主要聊的是consumers与produces两个属性

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
都可以设置多值

例子:返回JSON类型
@PostMapping(value ="/test1", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public TestRequest test1(@ModelAttribute() TestRequest testRequest) throws Exception {
System.out.println(testRequest);
return testRequest;
}

可以正常返回:

{
"name": "Angle",
"age": "24"
}

@PostMapping(value ="/test2", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public TestRequest test2(@ModelAttribute() TestRequest testRequest) throws Exception {
System.out.println(testRequest);
return testRequest;
}
返回报错
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver -Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

两种属性的区别:
consumes 标识处理request Content-Type为“application/x-www-form-urlencoded”类型的请求.
produces标识处理request请求中Accept头中包含了"application/x-www-form-urlencoded"的请求.
返回的内容类型为application/x-www-form-urlencoded;(返回类型与请求类型一致)
所以项目中使用了consumes 属性,因为请求的Content-Type与返回的Content-Type不一致
————————————————
版权声明:本文为CSDN博主「yangerkong」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kongkongyanan/article/details/106011293

 

posted @ 2024-01-23 13:11  沧海一滴  阅读(421)  评论(0编辑  收藏  举报