当Accept标头为*/*时使用@ResponseBody春季返回json会抛出HttpMediaTypeNotAcceptableException [英] Spring-Returning json with @ResponseBody when the Accept header is */* throws HttpMediaTypeNotAcceptableException

查看:128
本文介绍了当Accept标头为*/*时使用@ResponseBody春季返回json会抛出HttpMediaTypeNotAcceptableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring 3.0.0.

I'm using spring 3.0.0.

我有一个端点,该端点返回要序列化为JSON的对象.当请求带有Accept:application/json时,它可以正常工作.当前以*/*作为接受值进入请求.不幸的是,我无法控制该请求,否则我将对其进行更改.收到*/*时,它将引发HttpMediaTypeNotAcceptableException异常.

I have an endpoint that returns an object that I want to be serialized to JSON. When the request comes in with Accept: application/json, it works correctly. The request is currently coming in with */* as the Accept value. Unfortunately I don't have control over the request, otherwise I would change that. When */* is received, it throws a HttpMediaTypeNotAcceptableException exception.

有没有办法将此接受模式映射到application/json?

Is there a way to map this accept pattern to application/json?

这与另一个问题非常相似,但主要区别是我需要将Accept标头设为*/*. 无法通过适当的响应解决Spring的Json

This is very similar to another question, but the key difference is I need to have the Accept header be */*. Spring's Json not being resolved with appropriate response

这是我的控制器的样子:

Here's what my controller looks like:

@RequestMapping(value = "/v1/endpoint", method = RequestMethod.POST)
@ResponseBody
public EndpointResponse runEndpoint(@RequestBody String jsonData) {

    ObjectMapper mapper = new ObjectMapper();
    EndpointRequest opRequest = null;
    EndpointResponse opResponse = null;

    try {

        opRequest = mapper.readValue(jsonData, EndpointRequest.class);

        //....do stuff



    } catch (JsonParseException e) {
        return handleException(opResponse, e);
    } catch (JsonMappingException e) {
        return handleException(opResponse, e);
    } catch (IOException e) {
        return handleException(opResponse, e);
    }

    return opResponse;
}

谢谢!

推荐答案

从我今天收集的所有内容中,有2种解决此问题的方法.

From everything I've gathered today there are 2 options to get around this issue.

  1. 编写一个BeanPostProcessor,它将更新MappingJacksonConverter上受支持的mime类型以接受/. (使用supportedMimeTypes属性不起作用....请参见 https://jira.springsource. org/browse/SPR-6214

  1. Write a BeanPostProcessor that will update the supported mime types on the MappingJacksonConverter to accept /. (using the supportedMimeTypes property just doesn't work....see https://jira.springsource.org/browse/SPR-6214

这是我现在要使用的解决方案.这不是很漂亮,但是当您每次都期望application/json时,也都没有设置/的Accept标头.我最终将ServletResponse对象添加到我的方法中,并使用传统的直接写入输出流的方法来绕过消息转换器并返回我的响应.

Here's the solution I'm going with for now. It's not pretty, but neither is setting an Accept header of / when you are expecting application/json everytime. I ended up adding the ServletResponse object to my method and using the old school way of writing directly to the output stream to bypass the message converters and get my response returned.


@RequestMapping(value = "/v1/endpoint", method = RequestMethod.POST)
public void runEndpoint(@RequestBody String jsonData,
ServletResponse response) {

    ObjectMapper mapper = new ObjectMapper();
    EndpointRequest opRequest = null;
    EnpointResponse opResponse = null;
    StringWriter sw = new StringWriter();

    try {

        opRequest = mapper.readValue(jsonData, EndpointRequest.class);

        opResponse = ...do stff...

        mapper = new ObjectMapper();

        mapper.writeValue(sw, opResponse);

        response.setContentType("application/json");
        response.getOutputStream().write(sw.toString().getBytes());

        } catch (JsonParseException e) {
        handleException(opResponse, e, response);
        } catch (JsonMappingException e) {
        handleException(opResponse, e, response);
        } catch (IOException e) {
        handleException(opResponse, e, response);
    }
}

如果您有更优雅的选择,我很乐意看到!

If you have anything more elegant, I'd love to see it!

这篇关于当Accept标头为*/*时使用@ResponseBody春季返回json会抛出HttpMediaTypeNotAcceptableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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