0

I'm developing an httpclient application that use a list<OBJECT> from a RESTful web service in JSON format.

I want to use this result in JSON format.

This is my httpclient application class :

public class ClientAbortMethod {

    public final static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet("http://localhost:8080/structure/alldto");

            System.out.println("Executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {

              String data= EntityUtils.toString(response.getEntity());


                System.out.println(data);
                // Do not feel like reading the response body
                // Call abort on the request object
                httpget.abort();
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}

This is the result of executing this code

[{"ids":"#AB","champs":[ {"idChamp":1,"order":1,"type":"FROM"},{"idChamp":2,"order":2,"type":"TO"},{"idChamp":3,"order":3,"type":"TEXT"}]},{"ids":"#AC","champs":[{"idChamp":4,"order":1,"type":"FROM"},{"idChamp":5,"order":2,"type":"TO_MAIL"},{"idChamp":6,"order":3,"type":"TEXT"}]},{"ids":"tt","champs":[]}]

This is the restful web service

@RequestMapping("/alldto")
public List<StructureNotificationDto> GetALLStructureNotification() {
     return StructureNotif.StructureDTOS();
}

How can I get the result of this web service in JSON format or other easy-to-manipulate format?

rob
  • 6,147
  • 2
  • 37
  • 56
majed ben ali
  • 31
  • 1
  • 7

4 Answers4

0

You can use Jackson library. To add libraries, I suggest a dependency management system, like Maven. After that, use the following code:

   ObjectMapper mapper = new ObjectMapper();
   List<StructureNotificationDto> list = mapper.readValue(data, new TypeReference<List<StructureNotificationDto>>() {});

Then you can manipulate the data.

mtyurt
  • 3,369
  • 6
  • 38
  • 57
  • after adding Jackson and using this code , i get this error : – majed ben ali Apr 09 '15 at 23:39
  • Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.onlineproject.domain.StructureNotificationDto]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@5427c60c; line: 1, column: 3] – majed ben ali Apr 09 '15 at 23:39
  • after adding Jackson and using this code , i got this error : Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.onlineproject.domain.StructureNotificationDto]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@5427c60c; line: 1, column: 3] – majed ben ali Apr 09 '15 at 23:42
  • If you don't have final fields, just make sure that you have getter/setters for all methods and a constructor with no arguments (make the constructor private so no one else can you use it). If you have final fields, assign them dummy values in your private constructor. Jackson will do the rest. – mtyurt Apr 10 '15 at 04:09
0

Maybe you want to convert the JSON String into Java Object, so you can manipulate it easily. You can use org.json for that purpose.

Please refer to this question.

Community
  • 1
  • 1
0

You want to manually convert the JSON response String to Java object using Jackson, GSON. Here is the example for converting JSON string to Java Object using GSON and Jackson

Annamalai Thangaraj
  • 522
  • 1
  • 5
  • 10
0
ClientResponse response = resourse.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
if(response.getStatus() == 200)
    return response.getEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
ConfToggleBO togleObj = mapper.readValue(enitity, ConfToggleBO.class);
Andrew LaPrise
  • 3,373
  • 4
  • 32
  • 50