-1

I want to get JSON data from a REST API.

I wrote a simple Java class as Jersey client to connect to the web service but when I execute the class, he displays HTML, not JSON. knowing that the API is authentication with login and password and I don't know how I process. Here is the source code for the class I tried to write.

{
    public class RestClient {

    public static void main(String ar[]) throws Exception
    {

        String baseUrl="??????????????";
        String password="???????????";
        String user="?????????";       

        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);

        WebResource service = client.resource(baseUrl);
        service.path("j_spring_security_check");
        service.queryParam("j_username", user);
        service.queryParam("j_password", user);
        service.queryParam("ajax", "true");
        String out = service.accept(MediaType.APPLICATION_JSON).get(String.class);
        System.out.println(out);
    }
}

1 Answers1

0

As the other guys said it depends on the REST API. you're code looks o.k. but maybe try using the code below and insert query parameters into url like so: /?parmam1=value1&param2=value2

Client client = Client.create();
WebResource webResource = client.resource(url);
String jsonResult = webResource.accept("application/json").get(ClientResponse.class).getEntity(String.class);
Tal Joffe
  • 5,347
  • 4
  • 25
  • 31
  • Thank you @Tal Joffe ,I ask you how I can use basic authentication în jersey client – Mhedbi Mohamed Amin Feb 25 '16 at 16:40
  • in your code it looks like authentication is used via query parameters and on that case my example works. if you something more complex try this http://stackoverflow.com/questions/6774506/jersey-client-api-authentication – Tal Joffe Feb 28 '16 at 07:51