How to generate an xml file using webservices in Java? - java

Can we generate an xml file using webservices in Java, and if so how?

Generating XML files has nothing to do with webservices. The common SOAP based webservices communicate with messages written in XML. So to call a webservice, you'll have to create a XML document that implements some xml schema and send the xml document to the servers address. And you won't need files, usually the XML documents are created in memory and not written to files.
Apache Axis2 is a quite powerful library that takes care of most of the marshalling/unmarshalling and communication stuff.

There are two Java Web Service Standards:
Java API for XML Web Services (JAX-WS)
Java API for RESTful Web Services (JAX-RS)
Each spec has multiple implementations. GlassFish is the reference implementation for both these standards.
You can either interact directly with XML, or with POJOs that are converted to XML via an XML binding layer. The standard binding layer for JAX-WS and JAX-RS is Java Architecture for XML binding (JAXB).
For an example of a JAX-RS Webservice check out:
http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-15.html

Create a web service (e.g. using Java 6 annotations) and let the annotated method return a DOM tree transformed to a string.

Related

How to implement SOAP Endpoints that receive SOAP XML (SAAJ?) Instead of generated objects using CXF

We have currently implemented SOAP endpoints with CXF (Spring Boot). In accordance with the contract-first approach, we provide a WSDL, generate Java objects, and services from it (cxf-codegen-plugin) and finally implement the service interface.
This is all very nice and simple, but we only need the SOAP XML or the corresponding DOM tree. Not only is the conversion to Java objects unnecessary, we also have to convert them back to XML and lose some information from the original XML.
As far as I understand is SAAJ (SOAP with Attachments API for Java) exactly right here, or am I missing something? At most all resources about SAAJ are quite old and "low level". I cannot find good resources for that kind of approach.
In addition, we want to implement new services as microservices soon and are currently reviewing Quarkus and Apache Camel. However, I cannot find a (simple) way to create corresponding endpoints from the WSDLs, via which we then receive the SOAP message as plain XML / DOM tree.
In jax-ws, message-level access can be accomplished on the server side using web service Provider-based endpoints, and on the client side using Dispatch clients.
A web service Provider-based endpoint offers a dynamic alternative to the Java service endpoint interface (SEI)-based endpoint. Unlike the SEI-based endpoint that abstracts the details of converting between Java objects and their XML representation, the Provider interface enables you to access the content directly at the XML message level—without the JAXB binding. web service Provider-based endpoints can be implemented synchronously or asynchronously using the javax.xml.ws.Provider or com.sun.xml.ws.api.server.AsyncProvider interfaces, respectively.
See https://cxf.apache.org/docs/provider-services.html for details

How to use WSDL

I'm working on a WSDL application. But actually I didn't understand what makes the "?wsdl" parameter and what will I do with the returned XML. For example:
https://adwords.google.com/api/adwords/cm/v201309/CampaignService?wsdl
This URL returns and XML string but what will I do with this?
I can convert schema files to java classes using jaxb (xjc) but I didn't understand correctly how to use this WSDL?
Thanks for your answers.
WSDL(Web Services Description Language) is just a contract in the form of xml defining the web services. It contains the details of input and output params of webservices. It is used between client and server to define the interface of communication. It is analogous to a method signature in a programming language. But as it is used between hetrogenous systems so xml is used to describe it.
WSDL is a XML file which contains description of a SOAP web service. WSDL file contains details regarding XML request structure, XML response structure, Web service endpoint details, Web service URL etc.WSDL file is the single most important file of a SOAP web service. The owner of a web service provides WSDL file to the client and by using the WSDL file a client interacts with the service. By using the WSDL file a client can generate client side stubs and java classes to contact with the server. Java since its release 7 supports APIs to generate client side code from a WSDL file. WSDL is created at the time of creating the web service.
When you have that URL containing the WSDL,simply use a tool like SOAPUI and generate a SOAP message and invoke web service. Web service can contain one more input elements so in that case you need to provide input element in the SOAP message.
To invoke a web service you just need a WSDL URL , you already have that. Just use a tool like SOAPUI.
WSDL is the file describing your webservices.
Meta Information about your methods, etc.
Any Web Client should understand and use WSDL files to generates all methods needed to communicate with a Web Server.

Spring-ws VS Apache cxf VS Apache Axis2 VS Metro

I need to create soap web service. I already have wsdl and interface and implementation (as pojo).
I am now choosing between subj.
I need frame work that will:
Work as servlet in servlet container
Require only one servlet mapping in my web.xml
Have good spring integration (because my service implementation is spring bean)
No require me to add annotations. I do not have annotations on my interface or implementation.
Spring-ws: Looks cool, but as far as I understood it forces me to deal with XML directly which I do not want to do. I want framework to deserialize message and pass it as parameter to my POJO.
Apache cxf is powerful and has spring integration, but if I use Jax-WS frontend for it I will have to use annotations, and I do not want to touch my POJO. What about simple front-end?
Metro is Jax-WS RI, so it depends on annotations heavily.
Axis2 seems to be my choice. What would you choose?
Just wanna tell what I am trying to do:
Our app connects to remote service using SOAP. They gave us WSDL, we've generated proxy classes and DTO and all this stuff is packed in jar and stored in VCS. Now I need to write emulation for this web service. And I do not want to generate new DTO.
I think Spring WS would be a good fit for you given your requirements. You do not need to deal directly with the XML. Spring will serialize/deserialize many types of objects including all of your data transfer objects. They should just be annotated JAXB entities. Check out this information: http://static.springsource.org/spring-ws/sites/2.0/reference/html/server.html#d5e1063
If you already have a WSDL you can generate your DTO objects from that WSDL. There are several tools that do this including most IDE's.

Understanding the java soap stack - jaxb, jax-*, cfx, etc

Not having done any modern java in a while I am a bit overwhelmed by the plethora of acronyms around providing a soap service. Can you help me summarize what the following technologies are and how they relate to each other. Some of them are obvious but I am adding them to complete the picture:
XML
SOAP
HTTP/TCP (think transports)
XSD
WSDL
JAXB
JAX-WS, JAX-RS, etc.
CFX
Let me know if I've missed something important from the list that I need to add.
XML should be one of the obvious ones
SOAP is a protocol for creating remote procedure calls (web services)
HTTP/TCP transport protocols
XSD XML Schema Definition, defines the data types of your XML documents, useful for Schema validation and parsing to Java objects
WSDL web service definition language, a descriptor for SOAP based web services. Contains the operations you can call and the data (using XSD) to use. The WSDL puts it all together.
JAXB API for binding XML and Java types, so you can parse XML files to Java objects and vice versa
JAX-WS API for SOAP based web services
JAX-RS API for RESTful web services (alternative to SOAP)
CFX is a framework from Apache for web services. All other technologies above are just APIs / standards, CFX is an implementation of these.
XML
markup language favoured by SOAP webservices. You should be considering whether JSON meets your needs
SOAP
Used to mean Simplified Object Appliction Protocol, but really now means 'non-RESTful' web services
HTTP/TCP (think transports)
Webservices tend to use HTTP, but don't have to. Benefit of HTTP is ubiquity / firewall punching (via proxies)
XSD
XML schema Definition - a 'better' validation framework for XML than DOCTYPE. This is arguable. XSD is overly heavyweight for many applications
WSDL
Web Service Description Language - defines the methods/parameters of SOAP based webservices. Arguably too heavyweight for many applications
JAXB
Java XML Binding - allows XML <-> Java object <-> XML roundtripping. Can be difficult to work with for non-trivial examples
JAX-WS, JAX-RS, etc.
JAX-WS is the (large) family of webservice standards around SOAP
JAX-RS is the API for RESTful webservices.
CFX
???
REST
An alternative to SOAP webservices based on Roy Fieldings paper. Often considered simpler than SOAP and easier to use. But care is needed to implement correctly, Noteably HATEOAS (Hypertext As The Engine Of Application State) is often misunderstood.
JSON
Javascript Object Notation - an alternative data representation to XML based on Javascript data literals

Spring WS Client - How to create mapping POJO for WSDLs without using Axis

I'm creating a Spring WS client. I have a huge WSDL from a third-party company. They provide a set of classes that maps to their WSDL via the Axis' WSDL2Java. I don't want to use Axis or any dependencies from them.
Since this is a web service client, I'm free to choose any framework. My problem is how do I generate a mapping class for the WSDL without using Axis's WSDL2Java. Am I stuck with manipulating pure XML? Take note I'm using Spring WS.
Edit:
Is it true if the service provider has built their service with Axis 1, you're basically stuck with making a client that's also based on Axis 1? I read it from this answer JAX-WS client with Axis service. I thought web services are supposed to be decoupled or at least independent from the platform that it was created, allowing you to have a .Net based web service to be accessed from a Java based client and vice versa?
If you want to use Spring-WS, then your best bet is to use the wsimport tool that ships with Java 6 (or use the JAX-WS RI, if you're on Java5). This will generate JAX-WS stubs for the web service. Included in these stubs will be standard JAXB bindings for the WSDL's schema, and those can be used with Spring-WS (wsimport will generate other service stubs that you won't need for Spring-WS).
If you are only interested in generating the POJOs from the WSDL, I think you could just get the XSD from the WSDL and use XJC tool to generate only the JAXB beans. This would avoid generating useless JAX-WS stubs.

Categories

Resources