简体   繁体   English

Spring Rest Json Web服务与异常处理程序

[英]Spring rest Json web service with exception Handler

I am getting an unconditional issue. 我遇到一个无条件的问题。 I made my REST services in spring Json based. 我在春季的Json上进行了REST服务。 My service suppose to work with "Content-Type application/json" but whenever I made request without content-type then it throws an exception. 我的服务假定可以使用“ Content-Type application / json”,但是每当我发出不具有content-type的请求时,它都会引发异常。

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

However I am handling this exception with @ExceptionHandler , but spring is not able to handle this exception and my control is not coming in @ExceptionHandler method. 但是我正在使用@ExceptionHandler处理此异常,但是spring无法处理此异常,并且我的控件不在@ExceptionHandler方法中。

Please advice! 请指教! how I can make sure that my incoming request needs to send content-type header ?? 我如何确保我的传入请求需要发送内容类型标头?

I wanted to catch this exception in my @ExceptionHandler method so that I can send error message with error code. 我想在我的@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;
    }

Well, the first issue is that @ExceptionHandler annotations on your controller methods handle only the exceptions thrown from within your controller; 好吧,第一个问题是控制器方法上的@ExceptionHandler批注仅处理从控制器内部引发的异常。 the exception that you experience gets thrown before the controller method is invoked and therefore does not get handled. 您遇到的异常是在调用controller方法之前引发的,因此不会得到处理。

It would get handled if you had a case like this: 如果您遇到这样的情况,它将得到处理:

@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;
    }
}

I suspect you would have to extend ExceptionHandlerExceptionResolver that would process only HttpMediaTypeNotSupportedException types. 我怀疑您将不得不扩展仅处理HttpMediaTypeNotSupportedException类型的ExceptionHandlerExceptionResolver。 Or refer to this page and see if it helps. 或参阅此页面 ,查看是否有帮助。

Also, see whether you can use a newer version of Spring. 另外,请查看是否可以使用较新版本的Spring。

The @ExceptionHanlder annotation is not working because you have an invalid return type. @ExceptionHanlder批注不起作用,因为您的返回类型无效。 The return type can be a String, which is interpreted as a view name or a ModelAndView object. 返回类型可以是String,它被解释为视图名称或ModelAndView对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM