2016-04-10 57 views
0

我已經使用spring構建了一個休息Web服務。服務被調用時,我得到500服務器錯誤。Spring中的響應錯誤500(HttpStatus字段空指針)

Controller類:

@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(對於響應)

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; 
    } 



} 

錯誤是:

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) 

我無法理解錯誤發生的原因。請幫忙。

回答

1

您致電ResponseEntity.status()的方式無效。 它說

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

要解決這個問題嘗試從您的控制器的方法好像回到一個ResponseEntity:

@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; 
} 
+0

你是說在ResponseEntity上方法狀態不存在?如果是這樣,它確實存在http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html#status-org.springframework.http.HttpStatus- – user871199

0

我碰到了同樣的問題,它原來在Spring 4.3.1,這是固定的。 但是,我找不到JIRA問題。也許這是另一個修復的副作用。

如果您仍然有問題(或者其他人採取了這一措施),請使用4.3.1或更高版本重試。

+0

我是一個對倒票大失望。我正好碰到這個問題,升級到更新的Spring版本也可能幫助其他人解決這個問題,就像它爲我做的那樣。 –