带有null返回值的Spring Ajax @ResponseBody [英] Spring Ajax @ResponseBody with null returned values

查看:604
本文介绍了带有null返回值的Spring Ajax @ResponseBody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约50个使用@ResponseBody注释的控制器。

I have about fifty controllers that use @ResponseBody annotation.

像这样:

@RequestMapping(value = "/someUrl.controller", method = RequestMethod.GET)
public @ResponseBody Object getObject(@RequestParam("id") Long id) {
    Object object = provider.getObject(id);
    return object;
}

有时候getObject方法返回 null 。问题是在客户端我得到空响应而不是 null

Some times getObject method return null. The issues is that on client side I get empty response instead of null.

在初始实现中,我们有自定义的 JsonView 对象,它作为包装器而没有@ResponseBody注释。

In the initial implementation we have custom JsonView object that works as wrapper without @ResponseBody annotation.

像这样:

@RequestMapping(value = "/someUrl.controller", method = RequestMethod.GET)
public JsonView<Object> getObject(@RequestParam("id") Long id) {
    Object object = provider.getObject(id);
    return new JsonView(object);
}

所以工作正常。

我在你如何覆盖Jackson 2.0中的null序列化器?但不幸的是它只适用于POJO中的字段。

I have found some solution at How do you override the null serializer in Jackson 2.0? but unfortunatly it works only for fields in the POJOs.

你有什么想法吗?需要处理吗?

Have you any ideas how in it can be handled?

提前致谢!

推荐答案

这不是一个需要解决的小问题。

This is not a trivial problem to solve.

Spring有一个常见的模式,如果一个处理程序方法返回 null ,它就意味着表示处理程序已经处理了生成和编写适当的响应内容,并且不需要在前面采取进一步的行动。

Spring has a common pattern in which if a handler method returns null, it is meant to indicate that the handler has already dealt with producing and writing the appropriate response content and that no further action is necessary on the front.

Spring已在其中应用此模式RequestResponseBodyMethodProcesser @ResponseBody HandlerMethodReturnValueHandler 实现)。它检查返回值是否为 null 。它将请求设置为已处理。如果返回值不是 null ,它会尝试使用适当的 HttpMessageConverter 序列化它。

Spring has applied this pattern in its RequestResponseBodyMethodProcesser (the HandlerMethodReturnValueHandler implementation for @ResponseBody). It checks if the return value is null. It sets the request as handled. If the return value is not null, it attempts to serialize it with an appropriate HttpMessageConverter.

一个选项是创建自己的 @ResponseBodyNull 注释和相应的 HandlerMethodReturnValueHandler 做同样的事情除了处理 null 。请注意,您不一定能重复使用 RequestResponseBodyMethodProcess 中的代码,因为某些 HttpMessageConverters 将无法尝试使用 null

One option is to create your own @ResponseBodyNull annotation and a corresponding HandlerMethodReturnValueHandler which does the same except also handles null. Note that you can't necessarily reuse the code from RequestResponseBodyMethodProcess because some HttpMessageConverters would fail trying to use null.

另一个类似的选项是覆盖 RequestResponseBodyMethodProcessor 以接受 null (具有上述限制)并使用 RequestMappingHandlerMapping 明确注册,覆盖默认的 HandlerMethodReturnValueHandler 秒。你必须小心地这样做(即注册相同的),除非你想失去功能。

Another similar option is to override RequestResponseBodyMethodProcessor to accept null (with the limitations stated above) and register it explicitly with your RequestMappingHandlerMapping, overwriting the default HandlerMethodReturnValueHandlers. You have to do this carefully (ie. register the same ones), unless you want to lose functionality.

更好的解决方案IMO将在响应正文中处理 null 。如果 getObject 没有返回任何内容,那对我来说就好像是404。设置适当的响应代码并瞧!

The better solution, IMO, would be to not deal with null in the response body. If getObject doesn't return anything, that seems like a 404 to me. Set the appropriate response code and voila!

您始终可以将 HttpServletResponse 注入到处理程序方法中并执行类似的操作

You can always inject the HttpServletResponse into your handler method and do something like

Object object = getObject(..);
if (object == null) {
    response.getWriter().print("null");
    // also set the content type to application/json
}
return object;

假设您知道必须将其序列化为JSON。

Assuming you knew that this had to be serialized to JSON.

这篇关于带有null返回值的Spring Ajax @ResponseBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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