2011-03-22 98 views
7

我正在使用Spring MVC与控制器,我的问题是如何返回一个JSON响应,它不同于@ResponseBody对象返回并传递给JSON返回。Spring MVC返回JSONS和异常处理

为了进一步阐述,我对象叫 “的UserDetails” 其中有两个字段称为 “名称”, “EMAILADDRESS”

@ResponseBody UserDetails 

现在返回的JSON看起来像

{名: “TheUsersName”, EMAILADDRESS: “[email protected]”}

有什么办法恢复之前,我可以修改JSON(A LL jsons以所有控制器的所有方法)将添加“状态”字段,其他json数据将位于json中的“数据”键下。

而且我怎么一个JSON返回到前端时,从某处Java服务器抛出一个异常,json的应该有“状态:假”,并将异常名(至少应状态的一部分,虽然)

+0

也许是相关的:http://stackoverflow.com/questions/6014784/serialize-specific-exceptions-with-spring-mvc – Bozho 2011-05-16 10:33:24

回答

2

是。改为返回模型和视图。

public ModelMap getUserDetails() { 
    UserDetails userDetails; // get this object from somewhere 
    ModelMap map = new ModelMap()(; 
    map.addAttribute("data", userDetails); 
    map.addAttribute("success", true); 
    return map; 
} 

要添加例外,你可以用同样的方法使用键和成功= false。

+0

我看不到'ModelAndView'在这里增加了什么值。为什么不只是返回'ModelMap'? – skaffman 2011-03-22 19:50:15

+0

我不确定为什么我想使用ModelAndView,但是你给了我一个使用像魅力一样工作的地图(HashMap)的想法,并给了我所需的输出。但是,如何在失败情况下执行相同操作?然而,对于所有控制器和控制器中的所有功能,集中这种方式的想法是什么? – MilindaD 2011-03-22 20:24:36

+0

@skaffman你说得对。大脑放屁从返回的意见。 @milindaD ModelMap不需要密钥,但没有太大的区别。它实现了Map。 – 2011-03-22 20:27:22

14

创建响应类:

public class Response<T> { 
    T data; 
    boolean status = true; 

    public Response(T d) { data = d; } 
} 

然后返回从你的控制器:

@ResponseBody public Response getUserDetails) { 
    //... 
    return new Response(userDetails); 
} 

为异常你要返回一个对象,如:

public class BadStatus { 
    String errorMessage; 
    boolean status = false; 

    public BadStatus(String msg) { errorMessage = msg; } 
} 

@ExceptionHandler(Exception.class) 
public BadStatus handleException(Exception ex, HttpServletRequest request) { 
    return new BadStatus(ex.getMessage()); 
} 
+0

查看此主题以获取更详细的答案:http://stackoverflow.com/questions/5641091/spring-3-exception-handling-using-json – Sydney 2011-04-13 15:47:05

2

另一种解决方案(适用于弹簧3.1),这是较少入侵的

在Spring配置:

<bean id="jacksonConverter"  class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
<mvc:annotation-driven> 
    <mvc:message-converters> 
     <bean class="mypackage.MyMessageConverter" 
      p:delegate-ref="jacksonConverter"> 
     </bean> 
    </mvc:message-converters> 
</mvc:annotation-driven> 

的想法是提供自己的HttpMessageConverter委托给提供的杰克逊转换器。

public class MyMessageConverter implements HttpMessageConverter<Object> { 
// setters and delegating overrides ommitted for brevity 
@Override 
    public void write(Object t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, 
      HttpMessageNotWritableException { 
// t is whatever your @ResponseBody annotated methods return 
     MyPojoWrapper response = new MyPojoWrapper(t); 

     delegate.write(response, contentType, outputMessage); 
    } 
} 

这样你所有的pojos都包裹着一些你在那里提供的其他json。

对于例外情况,ericacm提出的解决方案是最简单的方法(记住使用@ResponseBody注释'BadStatus'返回类型)。

需要注意的一点是:你的json序列化的BadStatus也会经过MyMessageConverter,所以你需要在被覆盖的'write'方法中测试对象类型,或者让MyPojoWrapper处理它。