Spring MVC 中的多种内容类型 [英] Multiple Content-type in Spring MVC

查看:33
本文介绍了Spring MVC 中的多种内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我想知道,我的输入值是否有问题,或者我们的标题中不能有多个内容类型.

控制器:

<块引用>

@RequestMapping(value = "/addressees",产生 = APPLICATION_JSON_UTF8_VALUE,方法 = GET)

解决方案

是的,spring mvc 请求映射支持多种消费 MIME 类型,示例如下

@RequestMapping(value = "/something", method = PUT,消耗 = {APPLICATION_JSON_VALUE,APPLICATION_XML_VALUE},产生 = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})public SomeObject updateSomeObject(SomeObject acct) {返回 doStuff(acct);}

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

要了解更多,请参阅此链接 -

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

Can we have multiple content-type in Spring MVC request header? I'm passing:

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

through Postman to my API. Currently, I'm getting org.springframework.web.HttpMediaTypeNotSupportedException: Invalid mime type ....

I wanted to know, is there something with my input values, or we can't have multiple content-type in our header.

Controller:

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

解决方案

Yes, spring mvc request mapping supports multiple consumes MIME type , sample looks like

@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);
}

Add consumes part in requestmapping like - consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}

For know more, refer this link -

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

这篇关于Spring MVC 中的多种内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆