gpt4 book ai didi

json - @ControllerAdvice 无法返回 ResponseEntity(T 是 POJO)

转载 作者:行者123 更新时间:2023-12-02 04:37:46 31 4
gpt4 key购买 nike

我正在使用 Spring 4 MVC 创建 REST API。
我希望我可以在请求无效端点时提供默认的 JSON 错误对象。
我已经阅读了关于 @ControllerAdvice 和 @ExceptionHandler 的信息,我试图正确使用它们:

package com.yopyop.wackend.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.yopyop.wackend.controller.ErrorResponse;
import com.yopyop.wackend.service.NotFoundException;


@ControllerAdvice
public class ExceptionControllerAdvice {

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> exceptionHandler(Exception ex) {

ErrorResponse error = new ErrorResponse();
error.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());

if (ex.getClass()==NotFoundException.class) {
error.setMessage("ExceptionControllerAdvice() Not found");
error.setErrorCode(HttpStatus.NOT_FOUND.value());
return new ResponseEntity<ErrorResponse>(error, HttpStatus.NOT_FOUND);
}
else {
error.setMessage("ExceptionControllerAdvice() " + ex.getClass() + "(" + ex.getMessage() + ")");
error.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
}

return new ResponseEntity<ErrorResponse>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler({NoHandlerFoundException.class})
ResponseEntity<ErrorResponse> handleNoHandlerFoundException(NoHandlerFoundException ex,
WebRequest request) {

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

ErrorResponse error = new ErrorResponse();
error.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
error.setMessage("ExceptionControllerAdvice() " + ex.getClass() + "(" + ex.getMessage() + ")");

return new ResponseEntity<ErrorResponse>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

我的 ErrorResponse 类如下:
package com.yopyop.wackend.controller;

public class ErrorResponse {
private int errorCode;
private String message;

public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

我修改了 web.xml 以便抛出 NoHandlerFound 异常:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Wackend WebApp</display-name>
<servlet>
<servlet-name>wackend</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>wackend</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

尽管如此,当收到对未映射端点的请求时,我得到:
> 22:47:28.545 [http-nio-8080-exec-15] DEBUG
> o.s.web.servlet.DispatcherServlet - DispatcherServlet with name
> 'wackend' processing GET request for [/wackend/swagger-ui.html]
> 22:47:28.548 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method
> for path /swagger-ui.html 22:47:28.552 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method
> [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]
> 22:47:28.552 [http-nio-8080-exec-15] DEBUG
> o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of
> singleton bean 'defaultController' 22:47:28.553
> [http-nio-8080-exec-15] DEBUG o.s.web.servlet.DispatcherServlet -
> Last-Modified value for [/wackend/swagger-ui.html] is: -1 22:47:28.566
> [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception
> from handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.567
> [http-nio-8080-exec-15] DEBUG o.s.b.f.s.DefaultListableBeanFactory -
> Returning cached instance of singleton bean
> 'exceptionControllerAdvice' 22:47:28.567 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> 22:47:28.603 [http-nio-8080-exec-15] ERROR
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Failed to invoke
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> org.springframework.web.HttpMediaTypeNotAcceptableException: Could not
> find acceptable representation at
> org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:146)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
> ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE] [...] at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
> [na:1.8.0_101] at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
> [na:1.8.0_101] at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> [tomcat-util.jar:8.0.36] at java.lang.Thread.run(Thread.java:745)
> [na:1.8.0_101] 22:47:28.603 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception
> from handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.603
> [http-nio-8080-exec-15] DEBUG o.s.b.f.s.DefaultListableBeanFactory -
> Returning cached instance of singleton bean
> 'exceptionControllerAdvice' 22:47:28.603 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> 22:47:28.604 [http-nio-8080-exec-15] ERROR
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Failed to invoke
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> org.springframework.web.HttpMediaTypeNotAcceptableException: Could not
> find acceptable representation at
> org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:146)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
> ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] [...] at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
> [na:1.8.0_101] at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> [tomcat-util.jar:8.0.36] at java.lang.Thread.run(Thread.java:745)
> [na:1.8.0_101] 22:47:28.604 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from
> handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.604
> [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from
> handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.604
> [http-nio-8080-exec-15] DEBUG o.s.web.servlet.DispatcherServlet - Null
> ModelAndView returned to DispatcherServlet with name 'wackend':
> assuming HandlerAdapter completed request handling 22:47:28.604
> [http-nio-8080-exec-15] DEBUG o.s.web.servlet.DispatcherServlet -
> Successfully completed request

然后返回一个标准的Tomcat 404页面。
请不要那样:
  • 返回新的响应实体(错误,
    HttpStatus.INTERNAL_SERVER_ERROR);

    将返回一个带有空体的 HTTP 500(比 Tomcat 更好
    默认页面)
  • 返回新的响应实体(“woot”,
    HttpStatus.INTERNAL_SERVER_ERROR);将返回一个带有 woot 作为主体的 HTTP 500。

  • 看起来我的 ErrorResponse 类无法完成 JSON 映射。

    此外,我使用默认 Controller ,但我想它没用,因为我使用了@ControllerAdvice。我把它贴在这里以获取信息:
    package com.yopyop.wackend.controller;

    import javax.servlet.http.HttpServletRequest;

    import org.springframework.http.HttpHeaders;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.NoHandlerFoundException;

    /**
    * Default controller that exists to return a proper REST response for unmapped requests.
    */
    @Controller
    public class DefaultController {

    @RequestMapping("/**")
    public void unmappedRequest(HttpServletRequest request) throws NoHandlerFoundException {
    String uri = request.getRequestURI();
    String method = request.getMethod();
    HttpHeaders h = new HttpHeaders();

    throw new NoHandlerFoundException(method, uri, h);
    }
    }

    我的 REST API 也使用 ResponseEntity<> 返回并成功返回正确的 JSON 有效负载。

    最佳答案

    我不知道确切的答案是什么,但问题可能出在 JSON 配置上。起初,您的 @Controller 不是 @RestController 并且您没有明确指定生成的内容。我希望你在你的请求中这样做,所以 spring 知道该怎么做。我认为来自@Controller 的 mime 类型是继承的并用于转换。

    您可以尝试在 http 消息转换器中添加断点。在每个转换器中都有一个数学方法 supports尝试在此处捕获您的代码。检查哪个转换器返回 true。如果有多个通过条件,Spring 将使用第一个。通过堆栈跟踪,您还可以找到哪些转换器可用于您的请求以及需要哪种转换器。

    http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/AbstractHttpMessageConverter.html#supports-java.lang.Class-

    关于json - @ControllerAdvice 无法返回 ResponseEntity<T>(T 是 POJO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620479/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com