Java HTTP 状态 500 - ... IllegalArgumentException:当我在 spring 控制器中使用 ResponseStatus 注释和返回值时返回值类型未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27179875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:56:51  来源:igfitidea点击:

HTTP Status 500 - ... IllegalArgumentException: Unknown return value type when I use ResponseStatus annotation and return value in spring controller

javajavascriptweb-servicesrestspring-mvc

提问by gstackoverflow

I have wrote following controller method:

我编写了以下控制器方法:

    @RequestMapping(value = "/member/createCompany/uploadImage1", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public Long handle() {
        return 1l;
    }

When I request this method in browser I see following:

当我在浏览器中请求此方法时,我看到以下内容:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: Unknown return value type [java.lang.Long]

type Exception report

message Request processing failed; nested exception is java.lang.IllegalArgumentException: Unknown return value type [java.lang.Long]

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Unknown return value type [java.lang.Long]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
root cause

java.lang.IllegalArgumentException: Unknown return value type [java.lang.Long]
    org.springframework.util.Assert.notNull(Assert.java:112)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:68)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(Charac
    .........

Did I implement server side wrong or is it client side problem?

我是否实现了服务器端错误还是客户端问题?

Updateto njjnex answer.

更新到 njjnex 答案。

What should I do if I want to realize following logic inside controller method:

如果我想在控制器方法中实现以下逻辑,我该怎么做:

       if(someCondition){
           return 1l;
       }else{
         //I want to return 500 or 400 http code
       }

采纳答案by njjnex

Use @ResponseBodyannotation in this case. Without it Spring interpret your return value as a view name. But it cannot be long thats why you got error Unknown return value type

在这种情况下使用@ResponseBody注释。如果没有它,Spring 会将您的返回值解释为视图名称。但它不能很长,这就是为什么你出错的原因Unknown return value type

The @ResponseBody annotation is similar to @RequestBody. This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).

@ResponseBody 注释类似于@RequestBody。这个注解可以放在一个方法上,并指示返回类型应该直接写入 HTTP 响应主体(而不是放在模型中,或解释为视图名称)。

Spring Reference ResponseBody

Spring 参考响应体

If you want to return error code from ResponseBody use:

如果要从 ResponseBody 返回错误代码,请使用:

return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);

See also How to respond HTTP error in a @ResponseBody

另请参阅如何在@ResponseBody 中响应 HTTP 错误

回答by Haritha Perera

@RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody annotations and as a result, simplifies the controller implementation:

@RestController 是控制器的专用版本。它包括 @Controller 和 @ResponseBody 注释,因此简化了控制器实现:

@RestController
@RequestMapping("books-rest")
public class SimpleBookRestController {

@GetMapping("/{id}", produces = "application/json")
public Book getBook(@PathVariable int id) {
    return findBookById(id);
}

private Book findBookById(int id) {
    // ...
}


 }

I thinked that case (Unknown return value type). i thinks You're applied @Controllerannotation in this class. if you want to repair it , you will want apply @Controllerinstead @RestControllerannotatan.

我认为这种情况(未知的返回值类型)。我认为你@Controller在这个类中应用了注释。如果你想修复它,你需要应用@Controller而不是 @RestControllerannotatan。

refer this link,
https://www.baeldung.com/spring-controller-vs-restcontroller

请参阅此链接,
https://www.baeldung.com/spring-controller-vs-restcontroller