当前位置: 首页 > 知识库问答 >
问题:

@ControllerAdvice返回ResponseEntity失败(T是POJO)

娄德运
2023-03-14

我正在使用Spring4 MVC创建一个REST API。我希望能够在请求无效endpoint时提供一个默认的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

然后返回标准Tomcat404页。请不要说:

>

  • 返回新的ResponseEntity(错误,HttpStatus.internal_server_error);

    将返回一个带有空正文的HTTP500(比Tomcat默认页面更好)

    另外,我使用了一个默认控制器,但我猜它没有用,因为我使用了@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有效负载。

  • 共有1个答案

    淳于凯
    2023-03-14

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

    您可以尝试在http消息转换器中添加断点。在每个转换器中都有一个方法支持尝试在这里捕获代码。检查哪个转换器返回true。如果有多个条件通过,则Spring使用第一个条件。通过stacktrace,您还可以找到哪些转换器可以满足您的请求,以及需要哪种转换器。

    http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/abstracthttpmessageconverter.html#支持-java.lang.class-

     类似资料:
    • 我想在下面返回JSON。 {“名字”:“杰基”} 新来的春靴在这里。1天大。有没有合适的方法可以做到这一点?

    • 我有一个,它扩展了一个名为的基本所有这些所做的就是创建一个位置服务客户端 和

    • 问题内容: 在控制器中,我创建json数组。如果我还可以的话: 但我需要返回JSON数组和HTTP状态代码: Eclipse在XXX行中看到错误: 如何返回json + http回复?我的工作代码用于返回一个json对象+ http状态代码: 问题答案: 现在我回来了。我不知道更好的解决方案,但是可以。

    • 问题内容: 我试图在Windows 2008 R2中使用Kibana运行ElasticSearch。 我关注了这篇文章:在带有木瓜的Windows服务器上安装logstash 一步一步,但我得到的是: 当我去 我得到: 因此,似乎ElasticSearch正在运行,但是由于某些原因,Kibana无法连接到它。 ElasticSearch日志包含错误: 知道我在做什么错吗? 问题答案: 我也遇到过类

    • 我试图通过RESTendpoint将数据从一个服务器发送到另一个服务器: 服务器B启动此请求: 服务器A接收请求并返回文件: 因此,很明显,我必须将声明更改为: 或者到 然后错误消失了,但我丢失了HttpStatus。回复时是否有确认信息?这里的正确解决方案是什么?