【发布时间】:2011-07-15 23:26:17
【问题描述】:
我正在使用 spring 3.0.0。
我有一个端点,它返回一个我想序列化为 JSON 的对象。当请求带有 Accept: application/json 时,它可以正常工作。该请求当前以*/* 作为接受值。不幸的是,我无法控制请求,否则我会改变它。当收到*/* 时,它会抛出一个 HttpMediaTypeNotAcceptableException 异常。
有没有办法将此接受模式映射到应用程序/json?
这与另一个问题非常相似,但关键区别在于我需要将 Accept 标头设置为 */*。 Spring's Json not being resolved with appropriate response
这是我的控制器的样子:
@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;
}
谢谢!
【问题讨论】:
-
您是否尝试在 runEndpoint 方法中添加 headers="Accept=/" 参数?这是一个示例:krams915.blogspot.com/2011/02/…
-
@chris 感谢您的想法。我试了一下,仍然得到 HttpMediaTypeNotAcceptableException。不过这是有道理的。 @RequestMapping 上的 headers 参数用于将请求映射到特定的 Controller 方法,但不影响 Response 转换器到 Accept mime 类型的映射。
标签: spring spring-mvc