具有错误处理的Spring MVC Rest服务控制器正确完成了吗? [英] Spring MVC Rest Service Controller with Error Handling done right?

查看:135
本文介绍了具有错误处理的Spring MVC Rest服务控制器正确完成了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何正确实现一个应该作为REST服务的Spring Controller。特别是我想尝试使界面尽可能的RESTful。此外,我想使用HTTP错误代码,以便我的客户端可以采取相应的行动。

I was wondering how to correctly implement a Spring Controller which is supposed to serve as a REST Service. Especially I want to try and make the interface as RESTful as possible. Also i'd like to make use of HTTP Error codes so my Clients can act accordingly.

我想知道如何实现我的方法,所以如果一切正常,他们会返回JSON罚款(在响应的正文中)或抛出一个http错误代码以及为什么它不起作用的自定义原因(可能是来自DAO或数据库的错误)。但是我不确定哪一个是正确的方法?返回一个String并添加值以返回Model,或者返回一个HashMap并将我的东西放在那里?或直接返回对象?但那么如果发生错误并且我无法返回所述类怎么办?返回null而不是?
我发布了2-3种我可以想象的方式:

I was wondering how to implement my Methods, so they return JSON if everything works fine(in the body of the response) or toss a http error code as well as a custom reason why it didnt work(maybe errors that came from the DAO or the database). However I'm not sure which one is the right way? return a String and add the values to return to a Model, or return a HashMap and put my stuff in there? or return the objects directly? but then what if an error occures and i cannot return said Class? return null instead? I post 2-3 ways of doing it that i could imagine:

@RequestMapping(value="/addUser", method= RequestMethod.POST)
public String addUser(@RequestBody User user, HttpServletResponse response, Model model) throws Exception{

    try{
        userService.addUser(user);
        model.addAttribute("user", userService.getUser(user.getUsername(), user.getPassword()));
        return "user";
    }catch(Exception e){
        model.addAttribute("error", e.toString());
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
        return "error";
    }
}

或者更确切地说:

@RequestMapping(value="/addUser", method= RequestMethod.POST)
public @ResponseBody Map addUser(@RequestBody User user, HttpServletResponse response){
    Map map = new HashMap();
    try{
        userService.addUser(user);
        map.put("success", true);
        map.put("username", user.getUsername());
    }catch (KeyAlreadyExistsException e){
        map.put("success", false);
        map.put("Error", e.toString());
        response.sendError(HttpServletResponse.SC_FORBIDDEN, e.toString());
    }catch(Exception e){
        map.put("success", false);
        map.put("Error", e.toString());
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    }
    finally {
        return map;
    }
}

我意识到代码不是恰到好处但是我无法弄清楚如何使它成为它需要的方式。也许一些经验回复会有所帮助?支持已经

I realize code is not "just right" but i cannot figure out how to make it the way it needs to be. Maybe some experiences responses would help? Thx for the support already

推荐答案

您还可以使用 @ExceptionHandler Rest Controller中带注释的方法。

You could also catch your exceptions with @ExceptionHandler annotated methos within your Rest Controller.

@ExceptionHandler(Exception.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public String handleException(Exception e) {
    return "return error object instead";
}

这将使您的实际控制器/业务逻辑更清洁。

this will make your acutal controller/business logic cleaner.

这篇关于具有错误处理的Spring MVC Rest服务控制器正确完成了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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