Response Error 500 in Spring (HttpStatus field nul

2019-08-19 06:18发布

I have build a rest web service using spring. I am getting 500 server error when the service is called.

Controller class :

@RequestMapping(value = "/wordlist", method = RequestMethod.GET)  
     public ResponseEntity getList(@RequestHeader("wordid") int wordId)  
     {  
          ResponseList responseObejct = wordService.getList(wordId); 
          return ResponseEntity.status(responseObejct.getStatusCode()).body(responseObejct.getResponseWordList());
     }

DaoImplementation :

String listHql = "from Word where wordId > ? or wordId = ?";
        Query query = session.createQuery(listHql);
        query.setParameter(0, wordId);
        query.setParameter(1, wordId);
        query.setMaxResults(30);



        if(query.list().size()>0){
            response.setStatusCode(HttpStatus.OK);
            response.setResponseWordList((ArrayList<Word>)query.list());
        }else{
            response.setStatusCode(HttpStatus.NOT_FOUND);
        }
        session.getTransaction().commit();

ResponseList.java (for response)

public class ResponseList {

    private ArrayList<Word> responseWordList;
    private HttpStatus statusCode ;



    public ArrayList<Word> getResponseWordList() {
        return responseWordList;
    }
    public void setResponseWordList(ArrayList<Word> responseWordList) {
        this.responseWordList = responseWordList;
    }
    public HttpStatus getStatusCode() {
        return statusCode;
    }
    public void setStatusCode(HttpStatus statusCode) {
        this.statusCode = statusCode;
    }



}

Error is:

ava.lang.IllegalArgumentException: Can not set final org.springframework.http.HttpStatus field 
org.springframework.http.ResponseEntity.statusCode to java.util.ArrayList
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(UnsafeQualifiedObjectFieldAccessorImpl.java:38)
    java.lang.reflect.Field.get(Field.java:393)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:86)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    com.google.gson.Gson.toJson(Gson.java:586)
    com.google.gson.Gson.toJson(Gson.java:565)
    org.springframework.http.converter.json.GsonHttpMessageConverter.writeInternal(GsonHttpMessageConverter.java:199)
    org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:222)
    org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:80)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

I cannot understand the why the error is coming. Please help.

2条回答
唯我独甜
2楼-- · 2019-08-19 06:43

I ran into the same issue, and it turned out that this was fixed in Spring 4.3.1. However, I cannot find a JIRA issue for that. Maybe it is a side-effect on another fix.

If you still have the problem (or if anyone else steps into this), please try again with 4.3.1 or higher.

查看更多
forever°为你锁心
3楼-- · 2019-08-19 06:44

The way you are calling ResponseEntity.status() is not valid. It says

The method status(HttpStatus) is undefined for the type 
ResponseEntity

To fix this try returning a ResponseEntity from your controller method like :

 @RequestMapping(value = "/wordlist", method = RequestMethod.GET)  
 public ResponseEntity<ResponseList> getList(@RequestHeader("wordid") int wordId)  
 {  
      ResponseList responseObejct = wordService.getList(wordId); 
      ResponseEntity<ResponseList> responseEntity = new ResponseEntity<>(
            responseObejct, HttpStatus.OK);
      return responseEntity;
 }
查看更多
登录 后发表回答