Spring MVC 中的多个 Content-type

Multiple Content-type in Spring MVC

我们可以在 Spring MVC 请求 header 中有多个 content-type 吗? 我路过:

{Content-type = application/json, text/plain}

通过 Postman 到我的 API。目前,我得到 org.springframework.web.HttpMediaTypeNotSupportedException: Invalid mime type ....

我想知道,我的输入值是否有问题,或者我们的 header.

中不能有多个 content-type
Controller:

@RequestMapping(value = "/addressees", produces = APPLICATION_JSON_UTF8_VALUE, method = GET)

是的,RequestMapping.consumes 接受 Mime 类型数组

String[] consumes() default {};

请注意,您必须使用 consumes 来定义传入的 MIME 类型。 produces为传出型

您的请求 header 每个请求可以有一个 content-type。您向服务器指定实际发送的数据类型。

您的 server/API 端点可以支持多个。

因此,如果您的请求同时指定了application/json和text/plain,我认为您的请求有问题。

是的,spring mvc 请求映射支持多种消费 MIME 类型,样本看起来像

@RequestMapping(value = "/something", method = PUT, 
                consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}, 
                produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
public SomeObject updateSomeObject(SomeObject acct) {
    return doStuff(acct);
}

在请求映射中添加消耗部分,例如 - consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}

要了解更多信息,请参阅此 link -

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html