Select to view content in your preferred language

Invoking REST API from Java using Jersey

3657
1
11-04-2010 07:34 PM
RicoLelina
New Contributor
Hi,

I'm working on a proof-of-concept for adding features using the REST API. The first step I tried is to enter the following into the test page to add a Point:
url:
http://<host>/ArcGIS/rest/services/test/FeatureServer/1/addFeatures

request:
[{"geometry":{"x":102.0,"y":0.5},"attributes":{"prop1":"value1","prop2":"value2"}}]

response:
{"addResults" : [{"objectId" : 342, "globalId" : null, "success" : true}]}


Then I go to the map and see the Point I just added. So it appears to work.

The next step is to do the same thing from Java code which is itself a REST service: so from my REST service I call the addFeatures REST service using Jersey. I'm not going to post the entire source code but just some pertinent snippets.

The code below shows how I create a WebResource with the url for the addFeatures service as above.
    import javax.ws.rs.core.MediaType;

    import org.codehaus.jackson.JsonNode;
    import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
    import org.codehaus.jackson.node.ArrayNode;
    import org.codehaus.jackson.node.JsonNodeFactory;
    import org.codehaus.jackson.node.ObjectNode;

    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.WebResource;
    import com.sun.jersey.api.client.config.ClientConfig;
    import com.sun.jersey.api.client.config.DefaultClientConfig;

    private WebResource getResource(String url)
    {
        ClientConfig config = new DefaultClientConfig();
        config.getClasses().add(JacksonJsonProvider.class);
        Client client = Client.create(config);
        return client.resource(url);
    }


The following code shows how I invoke the addFeatures service. request is a JsonNode (I'm using Jackson). I print out request to verify that I'm passing a valid request, in this case I get the same string I entered above into the test page.
    System.out.println("request: " + request);
    String response =
                        resource
                            .queryParam("f", "json")
                            .entity(request, MediaType.APPLICATION_JSON)
                            .post(String.class);
    System.out.println("ArcGIS response: " + response);


However, the response I get is:
{"error":{"code":400,"message":"Unable to complete  operation.","details":["Invalid parameters"]}}


This is my first time calling a REST service from Java. I hope there is a simple explanation why I'm getting an error. Is there a log I can see on the ArcGIS server to see what input it's getting from my Java client?

Thanks,
Rico
0 Kudos
1 Reply
RicoLelina
New Contributor
I figured out my problem after looking at the HTTP traffic. The features data is passed in as a query parameter and not as form data.

String response =
                        resource
                            .queryParam("f", "json")
                            .queryParam("features", request.toString())
                            .post(String.class);
0 Kudos