SpringMVC consumes 和 produces 用法
所属分类 spring
浏览量 982
consumes="application/json"
只处理 content-type 为 application/json 的请求
produces="application/json"
只处理 Accept 为 application/json 的请求
@RequestMapping(value="/contentTypeCheck",consumes="application/json")
public String contentTypeCheck(){
return "only for content-type=application/json "+LocalDateTime.now();
}
@RequestMapping(value="/acceptCheck",produces="application/json")
public String acceptCheck(){
return "only for Accept=application/json "+LocalDateTime.now();
}
/contentTypeCheck
org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported"
curl 请求设置 head
curl -XGET -H 'Content-Type:application/json' http://127.0.0.1:8090/contentTypeCheck
返回结果Ok
only for content-type=application/json 2021-02-03T09:08:10.567
/acceptCheck
浏览器发送
Accept
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
注意 */*
完整代码
https://gitee.com/dyyx/springboothello/blob/master/src/main/java/demo/controller/ConsumesAndProducesController.java
curl实用技巧
上一篇
下一篇
Spring的BeanFactoryPostProcessor和BeanPostProcessor
yaml 和 properties 互相转换
springboot应用首次访问慢解决方法
SpringMVC 数据验证
AspectJ简介及实例
Jackson JSON的三种处理方式