In my java code i am getting an xml response (without an xsd) from a third party service call. But I know what possible tags can come in the response.So I have manually created java bean classes and annotated it with jaxb syntax, and them I am unmarshaling the got response to objects. It's working fine. But I have some scenarios where I will get french content in between the xml tags. In those cases it fails to unmarshal. So, how should I proceed?
Related
how can i capture the status and message tags from this xml response
You can use JAXB
In Spring 3, one of the feature of mvc:annotation-driven, is support for convert object to/from XML file, if JAXB is in project classpath.
Here SpringGuide for marshalling and demarshalling object to xml and vice versa.
I want to develop one web api,that will produce and consume both JSON and XML data.
I have used JAXB for XML support and it is working fine.
Now i want to add JSON type.i studied different tutorial, all are using different dependencies as below:
jersey-media-json-jackson
jersey-json
jersey-media-multipart
please help me whcih perfect and which support both XML and JSON.
what is difference between them all?
1. Jersey-Media-Json-Jackson
Jackson JSON processor could be controlled via providing a custom Jackson 2 ObjectMapper (or ObjectMapper for Jackson 1) instance. This could be handy if you need to redefine the default Jackson behaviour and to fine-tune how your JSON data structures look like. Detailed description of all Jackson features is out of scope of this guide. The example below gives you a hint on how to wire your ObjectMapper (ObjectMapper) instance into your Jersey application.
In order to use Jackson as your JSON (JAXB/POJO) provider you need to register JacksonFeature (Jackson1Feature) and a ContextResolver<T> for ObjectMapper,
2. Jersey-Json :
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T> providers distributed with jersey-json module. These providers enable using three basic approaches when working with JSON format:
POJO support
JAXB based JSON support
Low-level, JSONObject/JSONArray based JSON support
3. Jersey-media-multipart
The multipart in this module provide an integration of multipart/* request and response bodies in a JAX-RS runtime environment. The set of registered providers is leveraged, in that the content type for a body part of such a message reuses the same MessageBodyReader<T>/MessageBodyWriter<T> implementations as would be used for that content type as a standalone entity.
The following list of general MIME MultiPart features is currently supported:
The MIME-Version: 1.0 HTTP header is included on generated responses.
It is accepted, but not required, on processed requests.
A MessageBodyReader implementation for consuming MIME MultiPart
entities.
A MessageBodyWriter implementation for producing MIME MultiPart
entities. The appropriate #Provider is used to serialize each body
part, based on its media type.
Optional creation of an appropriate boundary parameter on a generated
Content-Type header, if not already present.
So I have a web service in NetBeans 8.1, for which I've written the wsdl with embedded XSD (and an external ref also).
Now I need to be able call the SOAP service on other instances of the application:
i.e. have instance X call a method on instance Y, as a secondary goal of the application.
I don't like to use NetBeans automatic SOAP client wizards, as I would be pointing to the very service which I'm building - it would potentially be a chicken and the egg type of thing during building. Secondly, I already have all the required JAXB types used by the web service, so it should be easy to construct a client right?
Well my trouble starts when I want to use JAXB to marshal my request object into a javax.xml.soap.SOAPBodyElement (my current strategy is to use SAAJ for the client part), but how to add a Schema to the marshaller? the schema is embedded in the wsdl, and I can't figure out how to reference it.
I figured that I could split out the schema part into a separate XSD file, but I'm missing an annotation option for #WebService, where I can provide an XSD file, just like I can provide a wsdl file (currently the 'wsdlLocation' points to both wsdl & xsd as it is embedded).
I guess I may have to live with not doing XSD validation on the client side(it is enabled server-side), as it seems tricky to get a Schema object from the wsdl - is that possible somehow?
You can read the .wsdl as an InputStream and transform it to a DOMResult. You can then get the "schema" node from the DOMResult and turn it into a DOMSource. With that, you can make a Schema object using the Source[] constructor.
I haven't got it to work myself, I had too many imports and it became hell to manage the namespaces. The only code I found on this was in "SOA Using Java Web Services" by Mark Hansen, chapter 7.5.1: Validation. I don't think I can put that code here, but all the code you should need for this use case is in there.
I have sample requests and responses for a RESTful service. How to create XSD from them? Is there an automated way or I should manually do that?
I understand that what you are actually asking for is creating and XSD out of example XML files.
If so using trang (http://www.thaiopensource.com/relaxng/trang.html) is one of the options available:
java -jar trang.jar Sample.xml Sample.xsd
I am using axis2 to expose a method of existing class as a web service (bottom-up approach). The method takes a complex object (non-primitive type) as a parameter and returns a complex object as well.
I understand that axis2 will try to generate the schema for me in the wsdl file when I expose the method as a web service, and I can get the wsdl file by visiting the web service url and append ?wsdl into the end of it.
But upon closer examination, some of the attributes of the complex type in the parameters are represented as xs:anyType in the schema part of the resulting wsdl. The attributes that are converted into xs:anyType is a List. The bad thing with this is that when I generate the stub code for the client code, the method signature to set that particular attributes will take in an object as a parameter i.e. setAttribute(Object obj).
So my solution to this is to use JAXB 2.0 to generate the xml schema of the classes I need and then, import the xsd into the wsdl file that is generated by axis2 (downloaded from the web service url + ?wsdl) and use the edited wsdl instead of the one automatically generated. This solution seems to be working well for the client side. The method signature to set the attributes generated by the stub code will take in the proper type i.e. setAttribute(AnotherComplexType abcd). And by using tcpmon, I can see that the xml that is sent from the client to the server seems to be correct.
However, this approach does not work well for the server side because axis2 does not use the JAXB 2.0 annotation to convert the xml received back into the classes that the exposed method will be able to process.
My question is, is there anyway to solve my problem? The possible ways I can think of is either to modify the way axis2 process the xml after receiving it (I'm okay with processing it manually if there is indeed a way), or to make axis2 work well with JAXB 2.0 annotation? Or maybe any other idea?
Note: I'm not using the JAX-WS part of axis2
In the end I solved this myself. I generated the stub code for server side using the wsdl, modify the messageReceivers to use the generated message receiver instead, write a wrapper class that implements the generated interface, write a converter to convert the object from generated types in the parameter of the wrapper class methods going to be exposed to my internal types and expose the wrapper class instead.