java.lang.IllegalStateException:不是JSON对象

时间:2017-03-08 07:14:33

标签: java json spring-mvc sonarqube sonarqube-api

我正在尝试使用SonarQube Web API从SonarQube获取json响应。在将JsonElement分配到JsonObject时,我收到此错误:

java.lang.IllegalStateException: Not a JSON Object

这是我的Java类

@Controller
public class SonarController {

    @Autowired
    JenkinsDataService jenkinsService;
    @Autowired
    SonarDataService sonarService;
    HttpEntity<String> entity;

    @RequestMapping(value = { "/sonar" })
    public void test(HttpServletRequest request, HttpServletResponse response,ModelMap model ) throws ServletException, IOException{
        try{
            String url= "http://sbstpvmlx818:9000/api/resources?metrics=coverage,sonar&depth=1&metrics=ncloc";
            RestTemplate restTemplate = new RestTemplate();

            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            entity = new HttpEntity<String>("parameters", headers);
            ResponseEntity<String> sonarResponse = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

            JsonElement jelement = new JsonParser().parse(sonarResponse.getBody());
            JsonObject  jobject = jelement.getAsJsonObject();
        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}

这是我的Json回复

[{"id":2772,"uuid":"AVeKDMBLuPlc0jzh2Dos","key":"com.testgroup.sara:platform","name":"platform","scope":"PRJ","qualifier":"TRK","date":"2017-03-03T12:04:35+0530","creationDate":"2016-10-03T15:47:53+0530","lname":"platform","version":"1.0","msr":[{"key":"ncloc","val":590737.0,"frmt_val":"590,737"}]},{"id":1,"uuid":"AVeJ89Y3uPlc0jzh2Doq","key":"com.testgroup.tbms:tbms","name":"Tbms","scope":"PRJ","qualifier":"TRK","date":"2017-03-06T12:32:57+0530","creationDate":"2016-10-03T15:20:41+0530","lname":"Tbms","version":"5.0","msr":[{"key":"ncloc","val":427062.0,"frmt_val":"427,062"}]},{"id":6156,"uuid":"AVqJnkVdkCpBdZtQIlHD","key":"com.testgroup.sara.services.xdm:xdmf","name":"sara Services :: XDMF","scope":"PRJ","qualifier":"TRK","date":"2017-03-01T16:59:57+0530","creationDate":"2017-03-01T16:55:35+0530","lname":"sara Services :: XDMF","version":"0.9","description":"sara :: SERVICES","msr":[{"key":"ncloc","val":5798.0,"frmt_val":"5,798"}]}]

这是错误

java.lang.IllegalStateException: Not a JSON Object: [{"id":2772,"uuid":"AVeKDMBLuPlc0jzh2Dos","key":"com.testgroup.sara:platform","name":"platform","scope":"PRJ","qualifier":"TRK","date":"2017-03-03T12:04:35+0530","creationDate":"2016-10-03T15:47:53+0530","lname":"platform","version":"1.0","msr":[{"key":"ncloc","val":590737.0,"frmt_val":"590,737"}]},{"id":1,"uuid":"AVeJ89Y3uPlc0jzh2Doq","key":"com.testgroup.tbms:tbms","name":"Tbms","scope":"PRJ","qualifier":"TRK","date":"2017-03-06T12:32:57+0530","creationDate":"2016-10-03T15:20:41+0530","lname":"Tbms","version":"5.0","msr":[{"key":"ncloc","val":427062.0,"frmt_val":"427,062"}]},{"id":6156,"uuid":"AVqJnkVdkCpBdZtQIlHD","key":"com.testgroup.sara.services.xdm:xdmf","name":"sara Services :: XDMF","scope":"PRJ","qualifier":"TRK","date":"2017-03-01T16:59:57+0530","creationDate":"2017-03-01T16:55:35+0530","lname":"sara Services :: XDMF","version":"0.9","description":"sara :: SERVICES","msr":[{"key":"ncloc","val":5798.0,"frmt_val":"5,798"}]}]
at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90)
at com.test.reldashboard.controller.SonarController.test(SonarController.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.test.reldashboard.filter.UriFilter.doFilter(UriFilter.java:64)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.test.reldashboard.filter.SessionFilter.doFilter(SessionFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:662)

3 个答案:

答案 0 :(得分:7)

读为JSONArray以读取jsonarray

 if (jelement instanceof JsonObject) {
    JsonObject  jobject = new JsonObject(jelement .getAsJsonObject());
 } else if (jelement instanceof JsonArray) {
    JsonArray  jarray =  new JsonArray(jelement.getAsJsonArray());
 }

答案 1 :(得分:5)

不是JSON对象:[{"id":2772,"uuid":"AVeKDMBLuPlc0jzh2Dos"..... 这里在你的API响应起点是JSONArray而不是JSONObject所以你得到java.lang.IllegalStateException: Not a JSON Object:请添加条件由@rathna回答如下

if (jelement instanceof JsonObject) {
    JsonObject  jobject = new JsonObject(jelement .getAsJsonObject());
 } else if (jelement instanceof JsonArray) {
    JsonArray  jarray =  new JsonArray(jelement.getAsJsonArray());
 }

答案 2 :(得分:0)

我认为rathna代码中可能存在一个小错误。由于jelement.getAsJsonObject()已经是JsonObject,因此构造函数是多余的。以下是适用于我的代码:

if (jelement instanceof JsonObject) {
    JsonObject  jobject = jelement.getAsJsonObject();
 } else if (jelement instanceof JsonArray) {
    JsonArray  jarray = jelement.getAsJsonArray();
 }