web-dev-qa-db-ja.com

java.lang.IllegalStateException:JSONオブジェクトではない

SonarQube Web APIを使用してSonarQubeからjson応答を取得しようとしています。 JsonElementJsonObjectに割り当てると、次のエラーが発生します。

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)
7
revan

jSONArrayとして読み取ってjsonarrayを読み取る

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

JSONオブジェクトではありません:[{"id":2772,"uuid":"AVeKDMBLuPlc0jzh2Dos".....ここでは、API応答の開始点はJSONObjectではなくJSONArrayだったので、Java.lang.IllegalStateException: Not a JSON Object:以下の@rathnaによって回答されたif条件を追加してください

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

rathna のコードに小さな間違いがあると思います。 jelement.getAsJsonObject()はすでにJsonObjectであるため、コンストラクターは冗長です。これが私のために働くコードです:

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