【问题标题】:Spring rest Json web service with exception Handler带有异常处理程序的 Spring rest Json Web 服务
【发布时间】:2015-05-13 01:35:41
【问题描述】:

我遇到了一个无条件的问题。我在 Spring Json 中创建了我的 REST 服务。我的服务假设可以使用“Content-Type application/json”,但是每当我提出没有 content-type 的请求时,它就会引发异常。

 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported

但是我正在使用 @ExceptionHandler 处理此异常,但 spring 无法处理此异常,并且我的控件未进入 @ExceptionHandler 方法。

请指教!如何确保我的传入请求需要发送内容类型标头??

我想在我的@ExceptionHandler 方法中捕获这个异常,以便我可以发送带有错误代码的错误消息。

@RequestMapping(value = "/login", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<ReponseWrapper> login(@Valid LoginRequest loginRequest,
        BindingResult results,) {
    //any code
}



 @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseStatus(value = HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    public @ResponseBody Map<String, Object> handleUnsupportedMediaTypeException(HttpMediaTypeNotSupportedException ex) throws IOException {
        Map<String, Object>  map = Maps.newHashMap();
        map.put("error", "Unsupported Media Type");
        map.put("cause", ex.getLocalizedMessage());
        map.put("supported", ex.getSupportedMediaTypes());
        return map;
    }

【问题讨论】:

  • 试试produces = "application/json;charset=UTF-8" 而不是consumes = MediaType.APPLICATION_JSON
  • 您是否在应用程序上下文中配置了任何异常解析器?
  • 您可以在请求到达控制器之前使用过滤器来验证请求,并检查内容类型以避免此类请求可能是其中一种方式。也许您可以尝试这种方法

标签: java json spring rest spring-mvc


【解决方案1】:

嗯,第一个问题是控制器方法上的 @ExceptionHandler 注释只处理从控制器内部抛出的异常;您遇到的异常在调用控制器方法之前被抛出,因此不会得到处理。

如果你有这样的情况会得到处理:

@Controller
public class TestController {

    @ResponseBody
    @RequestMapping(value = "/test", consumes = APPLICATION_JSON_VALUE, produces = 
        APPLICATION_JSON_VALUE)
    public ResponseEntity<ResponseWrapper> test(LoginRequest request)
        throws HttpMediaTypeNotSupportedException {
        throw new HttpMediaTypeNotSupportedException("");
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseStatus(value = HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    public @ResponseBody Map<String, Object> handleUnsupportedMediaTypeException(
        HttpMediaTypeNotSupportedException ex) throws IOException {
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Unsupported Media Type");
        map.put("cause", ex.getLocalizedMessage());
        map.put("supported", ex.getSupportedMediaTypes());
        return map;
    }
}

我怀疑您必须扩展仅处理 HttpMediaTypeNotSupportedException 类型的 ExceptionHandlerExceptionResolver。或参考this page 看看是否有帮助。

另外,看看你是否可以使用更新版本的 Spring。

【讨论】:

    【解决方案2】:

    @ExceptionHanlder 注释不起作用,因为您的返回类型无效。返回类型可以是String,解释为视图名或ModelAndView对象。

    【讨论】:

    • 这是不正确的。由于 OP 使用的是@ResponseBody,因此返回的地图会自动转换为 JSON 字符串。阅读更多关于它的信息..
    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多