简体   繁体   中英

Get response JSON object webservice RESTful java

When I access my URL http://localhost:8081/projectName/pathh/param to show me the JSON Object created

"Employee": [{
    "id": "param"
}]

This is my code in Java. I use Eclipse + Tom Cat Server.

@Path("/pathh")
@GET
    @Path("{parameter}")

    public Response getJSONObj(@PathParam("parameter") String parameter) {

        JSONObject jsonObj = new JSONObject();

        JSONArray jsonarray = new JSONArray();

        jsonObj.put("id", parameter);
        jsonarray.put(jsonObj);
        System.out.println(jsonarray);
        JSONObject jsonMain = new JSONObject();
        jsonMain.put("Employee", jsonarray);
        System.out.println(jsonMain.toString());
        System.out.println(Response.status(200).entity(jsonMain).build());
        return Response.status(200).entity(jsonMain).build();
        }

I get that error :

HTTP Status 500 - java.lang.IllegalArgumentException: wrong number of arguments

type Exception report

message java.lang.IllegalArgumentException: wrong number of arguments

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

package com.tests;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
@Path("/path/{parameter}")

public class Test1 {
    @GET
   public Response getJSONObj(@PathParam("parameter") String parameter) {

        JsonObject jsonObj = new JsonObject();
        JsonArray jsonarray = new JsonArray();

        jsonObj.addProperty("id", parameter);
        jsonarray.add(jsonObj);
        System.out.println(jsonarray);
        JsonObject jsonMain = new JsonObject();
        jsonMain.add("Employee", jsonarray);
        System.out.println(jsonMain.toString());
        System.out.println(Response.status(200).entity(jsonMain).build());
        return Response.status(200).entity(jsonMain.toString()).build();
        }

}

I have used Jersey as the Jax-RS API implementation. jersey-server and jersey-servlet are included as dependencies and web.xml has an entry for the jersey servlet mapping.

It would help if you could debug it and find the line it's failing on, or whether it never makes it into the method and you have a problem with your application wiring/annotations.

That said, I suspect that if you delete or comment out the line

System.out.println(Response.status(200).entity(jsonMain).build());

it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM