Spring mvc控制器空返回处理程序 [英] Spring mvc controller null return handler

查看:133
本文介绍了Spring mvc控制器空返回处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List< Country> getListOfCountries(){

return countryService.listAll();
}

它显示对象的json视图,但如果服务返回null,则我想显示一条错误消息,任何建议请问?

首先,即使这不直接回答问题,您的对象永远不会返回 null 而不是空集合 - 您可以在 Effective Java 2nd Edition ,Item 43 / p.201



因此,如果没有发现国家的情况是正常的它必须由客户端JS代码处理,它将检查计数并显示相应的消息。



如果出现问题,您可以抛出异常(如Biju指出的out +1) - 我相信服务人员应该抛出异常,因为它知道发生这种情况的原因,并且不会返回null。

我想补充一点,在Spring 3.2中(在Spring 3.2之前,返回响应体是 complicated )你可以设置一个 @ ExceptionHandler 将返回JSON并设置客户端稍后可以处理的HTTP状态代码。

  @RequestMapping(/ test)$返回一个自定义的JSON响应和一些错误代码。 b $ b @ResponseBody 
公开列表< Country> getListOfCountries(){
//假设您的服务抛出新的NoCountriesFoundException();
//出现错误时
return countryService.listAll();
}

@ExceptionHandler(NoCountriesFoundException.class)
ResponseEntity< String> test(){
返回新的ResponseEntity< String>(
很抱歉,我们的服务器还不知道任何国家。,
HttpStatus.I_AM_A_TEAPOT);
}

然后在JS代码中,您可以根据返回的状态代码。

另外,为了避免在不同的控制器中声明相同的 @ExceptionHandler ,在Spring 3.2中,你可以把 @ExceptionHandler @ControllerAdvice 注解类中。



有关详情,请参阅 http:// static .springsource.org / spring / docs / current / spring-framework-reference / htmlsingle /#mvc-exceptionhandlers http://www.springsource.org/node/3738 为3.2具体的事情


@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<Country> getListOfCountries() {

    return countryService.listAll();
}

It displays a json view of the object but if the service return null, then I want to display an error message, Any suggestions pls?

解决方案

First of all, even if this does not directly answer the question, your objects should never ever return null instead of empty collections - you can find the reasoning in Effective Java 2nd Edition, Item 43 / p.201

So, if the situation when no countries were found is normal it must be processed by the client JS code that will check the count and display the respective message.

If something has gone wrong you can throw an exception(as Biju has pointed out +1) - I believe that it's the service who should throw the exception because it knows the reason why it happened, and not to return null anyway.

I'd like to add that in Spring 3.2(in pre Spring 3.2 returning response body is complicated) you can set an @ExceptionHandler that will both return JSON and set the HTTP status code which can be later processed by the client. I think that returning a custom JSON response with some error code is most optimal here.

    @RequestMapping("/test")
    @ResponseBody
    public List<Country> getListOfCountries() {
        //assuming that your service throws new NoCountriesFoundException();
            //when something goes wrong
            return countryService.listAll();
    }

    @ExceptionHandler(NoCountriesFoundException.class)
    ResponseEntity<String> test() {
        return new ResponseEntity<String>(
                "We are sorry, our server does not know any countries yet.",
                HttpStatus.I_AM_A_TEAPOT  );
    }

Then in the JS code, you can do specific processing depending on the returned status code.

Also, to avoid declaration of the same @ExceptionHandler in different controllers, in Spring 3.2 you can put @ExceptionHandler inside a @ControllerAdvice annotated class.

For details, see http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-exceptionhandlers and http://www.springsource.org/node/3738 for 3.2 specific things

这篇关于Spring mvc控制器空返回处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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