Serialize POJO to JSON through a provider - java

What is a common way to serialize Java Beans to JSON in a automatic fashion? The #Produces(MediaType.APPLICATION_JSON) doesnt seem to handle the conversion under the hood. Do we create our own JSON Providers or is there another way? Currently im trying to handle it via the ContextResolver:
https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ContextResolver.html
It is my understanding that the #Produces annotation should help create the expected output if a java object is returned from one of the resource class methods.

You do not have to code the jackson based provider yourself.
In apache karaf you can simply install the feature aries-jax-rs-whiteboard-jackson.
If you are not using karaf you can install the bundles from the feature yourself:
https://github.com/apache/aries-jax-rs-whiteboard/blob/master/jax-rs.features/src/main/feature/feature.xml

Related

How to implement JSON functionality in a shared library without hard-wiring third party dependencies

I'm writing a class to provide some logging output in JSON format.
One issue is that the various Java projects I work on already use JSON and have dependencies on 3rd party libraries like gson or Jackson.
The work the class needs to perform is quite small and I figured it should be easy to avoid creating a new dependency on any particular JSON library, analagous to SLF4j which picks up whatever logging framework is already present.
My plan is that the class would pick up a JSON engine and use it, or throw a "missing JSON library" exception at start-up.
Does this already exists? I can't find anything on the net.
Is the Java JDK service provider java.util.ServiceLoader suitable?
It seemed easy enough to implement when I used it to plug in a java.nio.file.spi.FileTypeDetector for mime type detection with Apache Tika (best explained on this useful blog post.
You could try Class.forName(), passing the full name of the core class for Gson, Jackson, or whatever other JSON library is in use. If the class is not on the classpath, you will get a ClassNotFound exception. If you did this test only once, the cost would be reasonable.

Rest api testing using java POJO and jackson

I am new to webservices testing and i am trying to test a webservice which returns both a JSON as well as XML output .
my management wants me to do this with jackson jersery and other libraries for XML
i created a java client using jersey and i got the response . i am not sure how to validate this response against a POJO class .
i am not entitled to use sophisticated libraries like rest assured or json path . can some one explain me how will i validate/assert both json and xml response against a POJO step by step and make my life easy ?
kindly help
also list down the libraries needed for handling xml responses too ?
Karate is a new test-framework for testing web-services, and it supports both JSON and XML. You don't need to know Java and it does not need you to add any libraries. The documentation is extensive and the demo project has examples.
Disclaimer: am dev.

JSON 2 POJO offline

I would like to generate a Java object from JSON. All examples on the Internet use an ObjectMapper class and a Java class to serialize the JSON too.
The JSON is complicated, and I don't want to define the Java class by hand with annotations for properties.
I want to be able to generate the Java class with Jackson or GSON annotations with the JSON data with software.
The website jsonschema2pojo does this for you online. My JSON is offline in a private network where I cannot use jsonschema2pojo for a complicated JSON string.
How can I replicate the jsonschema2pojo code in my own environment? Is it easy to do with Jackson, or GSON?
I don't think Jackson or GSON have this support yet but lots of people have contributed code according to your requirement.
You can check:
https://github.com/astav/JsonToJava
or
https://github.com/wotifgroup/json2pojo
Moreover maybe you can try downloading that dependent jar manually and add it in your MAVEN repository manually.
You can use jsonschema2pojo offline. It can be used as a Maven plugin, a Gradle plugin, and command line tool or an Ant task.
At the bottom of the page at www.jsonschema2pojo.org you'll see links to help with each of these methods.

From WSDL to Java Objects - autogeneration?

I have the following requirements and thinking about how to best get java objects from a WSDL.
XML data comes from a public SOAP Webservice
I have to use JAXB
I want to automatically unmarshall the retrieved data to Java objects
Ideally I'd like to have java objects using JAXB Annotations. Are there any tools that I could combine to autogenerate these?
Sure, there are lots of ways to use JAX-WS (which uses JAXB for its XML binding) to generate a web service client.
You can execute it from the command-line:
http://www.mkyong.com/webservices/jax-ws/jax-ws-wsimport-tool-example/
As part of your Maven build:
https://jax-ws-commons.java.net/jaxws-maven-plugin/
Or from within your Eclipse environment:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jst.ws.cxf.doc.user%2Ftasks%2Fcreate_client.html
Yes, there are. With every jdk, there is an executable file called wsimport that does exactly what you want.
Here is an answer i gave to a similar question.

Parsing XML at RunTime based on XSD with JAXB 2.x

Have a requirement so want to check its feasibility.We are using JAXB 2.x for unmarshling XML in to out existing POJO classes.below is the process we are following..
Based on the XSD provided we have already created required classes using JAXB utility.
On Run time we are passing only XML file and we first validating the XML with the preexisting XSD and if success than will move ahead with the Unmarshling.
For XML creation same process but in reverse order.
Now there is one requirement so the client want that they can pass XML and XSD to the method being developed and this method should hand over the common object as return by parsing that XML and later on they will handle the process of mapping that generic Object with the specific classes.
My question is,Is it possible in Jaxb 2.x that based on the XSD supplied it will first create required classes on the fly and than will parse XML and hand over the result to the client module so they can do the mapping work or is there any way to achieve that functionality??
Thanks in advance
In EclipseLink MOXy (I'm the tech lead) we have a feature called Dynamic JAXB, that lets you bootstrap from an XML schema and use generic objects. You might find this approach useful:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic
Dynamic java bean from xsd
I know it Is doable. One student in my lab did almost the same thing on a different subject. Though, you will have to play with classloader to be able to load dynamically the classes you've jest created. Once that is done, you can parse the XML using the classloader that includes the new classes.
Of course, it is not as easy as it sounds like...
Have a look at the sample samples/inline-customize
located at http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/2.0/jaxb/samples.html

Categories

Resources