2

I am trying to write a java client for Restful web service implemented using WCF & NTLM Authentication.

I am able to use Apache HTTPClient library to connect and retrieve the data.

The following code gives me the response in the JSon text form.

DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostName, 443),    new NTCredentials(userName, password, hostName, domainName));
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 150000);

HttpGet httpget = new HttpGet(url);
httpget.setHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
content = EntityUtils.toString(responseEntity);

Now i am writing JSon parser to convert the JSon text into business objects(Manually created classes).

Does anyone knows how to automate the business object class creation and automated response parser to convert Json text to java object as we consume SOAP based Web Services using frameworks like Axis/CXF?

thangamanikasi
  • 846
  • 6
  • 19
  • 1
    Are you asking if a parser exists already that parses a JSon text directly to a Java object? Or are you asking for a way to create proxy classes from the service's WSDL schema? If the service exposes both SOAP and REST endpoints, you can get the WSDL by appending `?wsdl` to the service's URL. The generated DTOs should work both for the REST and SOAP endpoints – Panagiotis Kanavos Sep 25 '14 at 07:29
  • I need a framework to generate the DTO based on the service & converter from Raw JSon text to DTO when REST call is invoked. SOAP service is not exposed – thangamanikasi Sep 25 '14 at 08:08
  • But *can* you get the WSDL by appending `?wsdl` to the service URL, eg like `http://host/service.svc?wsdl` ? If you can, you can use one of the many tools that create service proxies to create the DTOs. If you can't, there's no schema to create your DTOs from. You could use a tool to create the DTOs from a JSon sample, assuming the sample contains all the DTOs you are going to encounter. – Panagiotis Kanavos Sep 25 '14 at 08:33
  • There is no wsdl. :-( – thangamanikasi Sep 25 '14 at 09:00

1 Answers1

1

There are several options for automatically converting JSON data to Java objects. Some options are:

  • Jackson - Annotate Java classes and fields to link them to the JSON schema.
  • Google GSON - Can create simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa. Works with existing classes without modifying source code.
  • JSON.org - Doesn't map the JSON directly to your Java classes, but provides parsing into JSONObject and JSONArray classes to make it simple work with JSON.

More answers can be found in this question: How to parse JSON in Java

EDIT All of the above require manually creating the Java objects, and then linking them to the JSON representation. If instead you want to automatically create the Java objects that correspond to the C# objects, I don't know of a way to do it automatically, however, the JSONSchema2POJO tool will generate Java POJOs from a sample of JSON, so that might save you some time.

Community
  • 1
  • 1
Richard Neish
  • 8,414
  • 4
  • 39
  • 69
  • Thanks Richard. Jackson solves my problem upto some level. But still there are some manual steps. Will post if i am able to automate completely. – thangamanikasi Oct 01 '14 at 03:45