@requestMapping中的produces属性详解

使用postman发送post请求:

produces:指定响应体返回类型和编码

使用前:

使用后:

 

 

官方文档: 

produces

public abstract java.lang.String[] produces
The producible media types of the mapped request, narrowing the primary mapping.

The format is a single media type or a sequence of media types, with a request only mapped if the Accept matches one of these media types. Examples:

 produces = "text/plain"
 produces = {"text/plain", "application/*"}
 produces = MediaType.APPLICATION_JSON_UTF8_VALUE
 
It affects the actual content type written, for example to produce a JSON response with UTF-8 encoding, MediaType.APPLICATION_JSON_UTF8_VALUE should be used.

Expressions can be negated by using the "!" operator, as in "!text/plain", which matches all requests with a Accept other than "text/plain".

Supported at the type level as well as at the method level! When used at the type level, all method-level mappings override this produces restriction.

See Also:

MediaType

Default:

{}

定义内容:MediaType类


Fields
Modifier and Type	Field and Description
static MediaType	ALL
Public constant media type that includes all media ranges (i.e.
static java.lang.String	ALL_VALUE
A String equivalent of ALL.
static MediaType	APPLICATION_ATOM_XML
Public constant media type for application/atom+xml.
static java.lang.String	APPLICATION_ATOM_XML_VALUE
A String equivalent of APPLICATION_ATOM_XML.
static MediaType	APPLICATION_FORM_URLENCODED
Public constant media type for application/x-www-form-urlencoded.
static java.lang.String	APPLICATION_FORM_URLENCODED_VALUE
A String equivalent of APPLICATION_FORM_URLENCODED.
static MediaType	APPLICATION_JSON
Public constant media type for application/json.
static MediaType	APPLICATION_JSON_UTF8
Public constant media type for application/json;charset=UTF-8.
static java.lang.String	APPLICATION_JSON_UTF8_VALUE
A String equivalent of APPLICATION_JSON_UTF8.
static java.lang.String	APPLICATION_JSON_VALUE
A String equivalent of APPLICATION_JSON.
static MediaType	APPLICATION_OCTET_STREAM
Public constant media type for application/octet-stream.
static java.lang.String	APPLICATION_OCTET_STREAM_VALUE
A String equivalent of APPLICATION_OCTET_STREAM.
static MediaType	APPLICATION_PDF
Public constant media type for application/pdf.
static java.lang.String	APPLICATION_PDF_VALUE
A String equivalent of APPLICATION_PDF.
static MediaType	APPLICATION_PROBLEM_JSON
Public constant media type for application/problem+json.
static MediaType	APPLICATION_PROBLEM_JSON_UTF8
Public constant media type for application/problem+json.
static java.lang.String	APPLICATION_PROBLEM_JSON_UTF8_VALUE
A String equivalent of APPLICATION_PROBLEM_JSON_UTF8.
static java.lang.String	APPLICATION_PROBLEM_JSON_VALUE
A String equivalent of APPLICATION_PROBLEM_JSON.
static MediaType	APPLICATION_PROBLEM_XML
Public constant media type for application/problem+xml.
static java.lang.String	APPLICATION_PROBLEM_XML_VALUE
A String equivalent of APPLICATION_PROBLEM_XML.
static MediaType	APPLICATION_RSS_XML
Public constant media type for application/rss+xml.
static java.lang.String	APPLICATION_RSS_XML_VALUE
A String equivalent of APPLICATION_RSS_XML.
static MediaType	APPLICATION_STREAM_JSON
Public constant media type for application/stream+json.
static java.lang.String	APPLICATION_STREAM_JSON_VALUE
A String equivalent of APPLICATION_STREAM_JSON.
static MediaType	APPLICATION_XHTML_XML
Public constant media type for application/xhtml+xml.
static java.lang.String	APPLICATION_XHTML_XML_VALUE
A String equivalent of APPLICATION_XHTML_XML.
static MediaType	APPLICATION_XML
Public constant media type for application/xml.
static java.lang.String	APPLICATION_XML_VALUE
A String equivalent of APPLICATION_XML.
static MediaType	IMAGE_GIF
Public constant media type for image/gif.
static java.lang.String	IMAGE_GIF_VALUE
A String equivalent of IMAGE_GIF.
static MediaType	IMAGE_JPEG
Public constant media type for image/jpeg.
static java.lang.String	IMAGE_JPEG_VALUE
A String equivalent of IMAGE_JPEG.
static MediaType	IMAGE_PNG
Public constant media type for image/png.
static java.lang.String	IMAGE_PNG_VALUE
A String equivalent of IMAGE_PNG.
static MediaType	MULTIPART_FORM_DATA
Public constant media type for multipart/form-data.
static java.lang.String	MULTIPART_FORM_DATA_VALUE
A String equivalent of MULTIPART_FORM_DATA.
static java.util.Comparator<MediaType>	QUALITY_VALUE_COMPARATOR
Comparator used by sortByQualityValue(List).
static java.util.Comparator<MediaType>	SPECIFICITY_COMPARATOR
Comparator used by sortBySpecificity(List).
static MediaType	TEXT_EVENT_STREAM
Public constant media type for text/event-stream.
static java.lang.String	TEXT_EVENT_STREAM_VALUE
A String equivalent of TEXT_EVENT_STREAM.
static MediaType	TEXT_HTML
Public constant media type for text/html.
static java.lang.String	TEXT_HTML_VALUE
A String equivalent of TEXT_HTML.
static MediaType	TEXT_MARKDOWN
Public constant media type for text/markdown.
static java.lang.String	TEXT_MARKDOWN_VALUE
A String equivalent of TEXT_MARKDOWN.
static MediaType	TEXT_PLAIN
Public constant media type for text/plain.
static java.lang.String	TEXT_PLAIN_VALUE
A String equivalent of TEXT_PLAIN.
static MediaType	TEXT_XML
Public constant media type for text/xml.
static java.lang.String	TEXT_XML_VALUE
A String equivalent of TEXT_XML.

也可以直接使用字符串:

    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8",method = {RequestMethod.GET,RequestMethod.POST})

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值