java - Spring rest controller @ExceptionHandler 返回 xml 内容和 json 错误

标签 java spring spring-restcontroller

所以我有一个@RestController,我想根据前端应用程序的模式返回并验证 XML,以便在编辑器中显示它们。我希望错误是 json 格式,以便用 js 处理和显示它们。

@RestController
public class UserController {

    @RequestMapping(value = "/test",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_XML_VALUE)
    public ResponseEntity<String> throwException(
        @RequestParam(value = "flag", defaultValue = "false") Boolean flag
    ) throws Exception {
        if (flag) {
            throw new Exception();
        } else {
            return ResponseEntity.ok("<xml>hello</xml>");
        }
    }


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(Exception.class)
    @ResponseBody
    ServerError exceptionHandler(HttpServletRequest req, Exception ex) {
        return new ServerError(req.getRequestURL().toString(),ex);
    }

}

我想以 JSON 格式返回的 ServerError :

public class ServerError {

    public final String url;
    public final String error;

    public ServerError(String url, Exception ex) {
        this.url = url;
        this.error = ex.getMessage();
    }

    public String getUrl() {
        return url;
    }

    public String getError() {
        return error;
    }
}

所以 <xml>hello</xml>返回就好了,但是当我将标志设置为 true 时我明白了

ERROR   2017-10-18 12:56:53,189 [http-nio-0.0.0.0-8080-exec-2] org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver  - Failed to invoke @ExceptionHandler method: eu.openminted.registry.core.exception.ServerError eu.openminted.registry.service.UserController.malformedExeption(javax.servlet.http.HttpServletRequest,java.lang.Exception)
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

此外,设置 produces对 XML 和 JSON 产生相同的结果

@RequestMapping(value = "/test",
        method = RequestMethod.GET,
        produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_UTF8_VALUE})

最佳答案

我设法通过从 @RequestMapping 中删除 produces 并使用 ResponseEntity 指定我要返回的类型来解决这个问题

@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<String> throwException(
    @RequestParam(value = "flag", defaultValue = "false") Boolean flag
) throws Exception {
    if (flag) {
        throw new Exception();
    } else {
        ResponseEntity response = ResponseEntity.ok().
            contentType(MediaType.APPLICATION_XML).
            body("<xml>hello</xml>");
        return response;
    }
}

该解决方案的问题在于,所有方法都有一个 @annotation 以及它们生成的类型,而这却没有,这破坏了一致性。

关于java - Spring rest controller @ExceptionHandler 返回 xml 内容和 json 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46808109/

相关文章:

java - Oracle JDBC DriverManager.getConnection() 挂起

java - 自定义 CacheResolver 不工作

java - 将属性传递给 Spring Security 应用程序中的 View

java - 将 Spring boot 与 rest Controller 一起使用

java - 在 Spring Boot 中处理异常的正确方法

Java JVM 堆大小

java - http.nonProxyHosts 仅在本地主机上被忽略

java - 部署到tomcat时出错

java - 如何在 Spring 中开发可以作为 REST 和 SOAP 公开的 Web 服务?

JAVA - JSON 将枚举列表序列化为类