2025年PostMappering中consumes与produces属性的作用

PostMappering中consumes与produces属性的作用哈喽大家好今天跟大家简单聊一聊 PostMapperin 中 consumers 与 produces 两个属性的作用 在对接接口中 对方 API 要求 请求头 HTTP Header 中设置 Content Type 为 application x www form urlencoded 响应头 HTTP Header 中 Content Type 为

大家好,我是讯享网,很高兴认识大家。

哈喽大家好今天跟大家简单聊一聊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不一致

小讯
上一篇 2025-01-18 13:15
下一篇 2025-02-23 13:57

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/128013.html