Using webservices with javax.ws or javax.jws - java

I'm starting to choose a way to create my webservice, so i found 2 ways to do it:
1) Using the package javax.jws, with annotation #WebService:
#WebService(...)
public class MyServiceImpl {
2) The other way is using javax.ws, with annotation #Path:
#Path("/MyService")
public class MyServiceImpl
In my understand using the second solution is more simple, because when i need to create the Client i just need make a HTTP call (GET/POST). Using the first solution i need create a WSDL client, more complex solution.
So, I would like to know which is the advantage in use FIRST SOLUTION.

The SOAP/WSDL style is useful when a formal contract must be established to describe the interface that the web service offers.The Web Services Description Language (WSDL) describes the details such as messages, operations, bindings, and location of the web service.
Also the SOAP/WSDL style is useful when the application architecture needs to handle asynchronous processing and invocation (e.g. using JAX-WS the assynchronous clients can be created).
The disadvantages of SOAP/WSDL style are
its complexity: tools are required to create a client
heavier on Bandwidth : SOAP requires a heavy XML wrapper arround each request or response
complicated security rules
The advantages of REST style are
simplicity: REST client can be accessed from any browser (however, this is only true for GET method. Data creation request requires also the XML wrapper).
lighter on Bandwidth : data on the wire are usually bare xml elements (not wrapped within the <Envelope><Body> tags).
REST application security rules can be setup using the http standards: the administrator (or firewall) can discern the intent of each message by analyzing the HTTP command used in the request.
For example, a GET request can always be considered safe because it can't, by definition, modify any data.
The disadvantage of REST style is that it still doesn't cover all business requirements
There is no common standard accepted yet for the formal REST service description
REST requests (especially GET method) are not suitable for large amount of data
REST doesn't cover all web services standards, like Transactions, Security, Addressing, Trust, Coordination,

Related

A real time practical situation where SOAP web service is preferred over REST?

I read a lot about why REST is preferred over SOAP and a lot of practical situations for that. But I could not find an article/answer on When SOAP can be preferred over REST. Can anyone please explain me a real time practical situation where SOAP can be preferred over REST?
NB - Please don't mark this question as duplicate without reading the question fully. This is not a duplicate of REST vs SOAP.
NB - Please don't mark this question as duplicate without reading the question fully. This is not a duplicate of REST vs SOAP.
when should you use SOAP? Representational State Transfer (REST) implements the standard HTTP/HTTPS as an interface allowing clients to obtain access to resources based on requested URIs. It is important to note that REST based services are stateless because http/https is natively stateless.
One of the many benefits for implementing HTTP/HTTPS as an interface is can be found in caching. Caching can be done on a web service much like caching is done on requested web pages. Caching allows for reduced web server processing and increased response times because content is already processed and stored for immediate access. Typical actions performed by REST based services include generic CRUD (Create, Read, Update, and Delete) operations and operations that do not require state.
Simple Object Access Protocol (SOAP) on the other hand uses a generic interface in order to transport messages. Unlike REST, SOAP can use HTTP/HTTPS, SMTP, JMS, or any other standard transport protocols. Furthermore, SOAP utilizes XML in the following ways:
Define a message
Defines how a message is to be processed
Defines the encoding of a message
Lays out procedure calls and responses
As REST aligns more with a Resource View, SOAP aligns more with a Method View in that business logic is exposed as methods typically through SOAP web service because they can retain state. In addition, SOAP requests are not cached therefore every request will be processed by the server.
As stated before Soap does retain state and this gives it a special advantage over REST for services that need to preform transactions where multiple calls to a service are need in order to complete a task. Additionally, SOAP is more ideal for enterprise level services that implement standard exchange formats in the form of contracts due to the fact that REST does not currently support this.
A real world example of where SOAP is preferred over REST can be seen in the banking industry where money is transferred from one account to another. SOAP would allow a bank to perform a transaction on an account and if the transaction failed, SOAP would automatically retry the transaction ensuring that the request was completed. Unfortunately, with REST, failed service calls must be handled manually by the requesting application.
References:
Francia, S. (2010). SOAP vs. REST. Retrieved 11 20, 2011, from spf13: http://spf13.com/post/soap-vs-rest
Rozlog, M. (2010). REST and SOAP: When Should I Use Each (or Both)? Retrieved 11 20, 2011, from Infoq.com: http://www.infoq.com/articles/rest-soap-when-to-use-each

Which design approach to reduce code duplication

I am trying to write code to invoke external services using java Httpurlconnection and noticed code is getting duplicated , but I am confused now whether to duplicate code or use lots of if-else block and write everything in single place.
Following are scenarios or type of services needs to be invoked -
1. Post/put/get Rest service with basic authentication over http protocol
2. Post/put/get Rest service with basic authentication over Https protocol
3. Post/put/get services with different oauth authentication options, here one more call to get token is required.
4. Soap services with different authentication method.
The response processing logic also differs for different type of services.
As of now I have written separate methods for each of these, but this is not reusable as it's tightly coupled with type of service.
Can anyone suggests better design approach for this?
You can abstract iteraction with API into separate service or services. For example TaskApi with getTasks(), sendTask(Task task), etc. methods. Look at Retrofit library, it much more convenient than using HttpURLConnection and helps you to solve authentication issues easy.

JAX-WS WebService, Addressing, MTOM and RespectBinding Features Use Cases

I am learning web services in Java. I would like to know the uses for the following 4 classes with the help of a very simple use case - WebServiceFeature, AddressingFeature, MTOMFeature, RespectBindingFeature.
Suppose, I am publishing a web service which will list all States in the United States. How would I utilize these 4 classes?
Java API for XML-Based Web Services (JAX-WS) Version 2.1 introduced the concept of features as a way to programmatically control specific functions and behaviors.
WebServiceFeature as per Java Doc
A WebServiceFeature is used to represent a feature that can be enabled or disabled for a web service.
The JAX-WS specification will define some standard features and JAX-WS implementors are free to define additional features if necessary. Vendor specific features may not be portable so caution should be used when using them.
AddressingFeature as per Java Doc
AddressingFeature represents the use of WS-Addressing with either the SOAP 1.1/HTTP or SOAP 1.2/HTTP binding. Using this feature with any other binding is undefined.
This feature can be used during the creation of SEI proxy, and Dispatch instances on the client side and Endpoint instances on the server side. This feature cannot be used for Service instance creation on the client side.
MTOMFeature as per Java Doc
This feature represents the use of MTOM with a web service.
Also
JAX-WS supports the use of SOAP Message Transmission Optimized Mechanism (MTOM) for sending binary attachment data. By enabling MTOM, you can send and receive binary data optimally without incurring the cost of data encoding needed to embed the binary data in an XML document.
RespectBindingFeature as per Java Doc
This feature clarifies the use of the wsdl:binding in a JAX-WS runtime. This feature can be used during the creation of SEI proxy, and Dispatch instances on the client side and Endpoint instances on the server side. This feature cannot be used for Service instance creation on the client side.
This feature is only useful with web services that have an associated WSDL.
Use Cases - unfortunately, see the Java Docs - I only say unfortunately as with me developing web services for several years now - I've never needed to use "WebServiceFeature,AddressingFeature, MTOMFeature, RespectBindingFeature" - they're for niche use cases which I dont believe most developers need to deal with. The most beneficial one I see would be the MTOMFeature but if you simply want a web service that lists all of the United States - you wouldn't likely need anything that elaborate.
Some Use Case Findings
MTOM: Using JAX-WS, you can send binary attachments such as images or files along with web services requests. With your example of States - you could have a web service that request that sets/updates each a picture of the states state bird and a base64 encoded audio file of the state song - the picture and song could be MTOM attachments.
AddressingFeature: This looks like it's just used when either the web service or web service client need to use WS-Addressing. I'd imagine trying to integrate with a third party web service that requires the use of WS-Addressing in which case your client you write would need to specify it's use
RespectBindingFeature: You can use the RespectBindingFeature to control whether a JAX-WS implementation is required to respect the contents of a Web Services Description Language (WSDL) binding that is associated with an endpoint. By implementing the feature, RespectBindingFeature, you have specified to enforce adherence of the contents of a WSDL binding that is associated with an endpoint for your JAX-WS application. The actual enforcement of the use of the WSDL document specifications, when they are provided, at run time has not been well defined in versions of the JAX-WS specification previous to Version 2.1.
WebServiceFeature: this is the parent class for the other features. The use case I imagine would only be the facts that the sub-classes inherit from it.

How REST is "lightweight"?

I have been seeing SOAP is "Heavy Weight" and REST is "Light Weight". On what parameters, we are telling REST is lightweight than SOAP?
We were using IFW model web services in our company earlier. But our management told us to develop all the new APIs going forward in REST. We are backend service providers in my company.
How REST is best useful for us?
What does "lightweight" means in context?
This question seems like repetition but don't understand the terms used.
REST gives you a session less window into a system. It does not track you, it does not care about you. All you have done is send a request which contains..hopefully some id to verify that you can make it. It may return a HTTP status code, it may return some body but ultimately, once the request is complete you are forgotten.
SOAP is heavy in a sense that it describes a "contract" between the remote system and your client. In order for your client to communicate effectively it MUST implement its schema...this is the SOAP skeleton. It describes the calls you can make and the objects you can expect back.
The reason why SOAP is heavy is because of serialization. Upon each SOAP request you typically serialize a java object, send it over HTTP and get a serialized response which is deserialized into an object via reflection...this is heavy. It also means that if the endpoint changes how they work, you must change your contract. You don't have to do this with REST.
With SOAP you run into multi threaded issues.
To answer quickly..
they might mean that a REST service is "lightweight" because you do not need to release changes to clients. You simply make changes to your logic, retaining URLS and the response should remain the same.
With SOAP...if you added a new field to an object, you would have to get the client to implement the new schema. SOAP is pretty old had.
REST is lightweight in that it and relies upon the HTTP standard to do its work. It is great to get a useful web service up and running quickly. If you don't need a strict API definition, this is the way to go. Most web services fall into this category. You can version your API so that updates to the API do not break it for people using old versions(as long as they specify a version). REST essentially requires HTTP and is format-agnostic, so you can use XML, JSON, HTML etc).
But the SOAP will wrap the structure into a SOAP envelope (follows an XML standard). The complexity of the SOAP envelope is based on the used message version and additional Web Service protocols. SOAP is generally transport-agnostic, meaning you don't necessarily need to use HTTP.
It can be pointed out that SOAP was designed for a distributed computing environment whereas REST was designed for a point-to-point environment.
I wonder if it is OK to say that while SOAP gives more security, REST-based API s will be easier on the resources and more scalable? As an example, Twitter, Facebook, Google Drive, Blogger, etc all have REST-based APIs that clients can consume.
Generally REST reduces the size and scope of request payloads by mapping request semantics to the underlying HTTP protocol. For example, SOAP will usually add an envelope (of varying complexity) which utilizes a WSDL (a contract) for both request and response types and service mappings. REST will just use POST, GET, etc to a URL with some HTTP encoded parameters and thus lacks an enforced contract.
There are several aspects of lightweight. SOAP is XML only, while REST allows you to send any payload, such as JSON which is less verbose and simpler than XML. Generally speaking, it takes less memory and less bandwidth to deal with JSON than XML.
On another level, using SOAP you typically describe everything as services. So, you need to define a message schema/structure for each verb and noun i.e. "CreateOrder". In REST, you use the predefined HTTP methods such as POST and put the noun/"resource" in the URI which uses already existing constructs on the HTTP level - not reinventing them in XML.
Then there is the effort it takes to adapt to change. A small change in SOAP requires you to redefine the schema, redefine the wsdl and handle versions. In rest, in the best of worlds, there is a degree of dynamic coded into the API (links to referenced resources, among other things), so that some changes can be implemented and takes effect directly.
All the heavy weight strictness of SOAP of course has a valid reason for existence. It's less likely two implementations have different interpretations of the API, than in a loosely defined REST API. SOAP also allows you to implement skeleton code in various languages based on a WSDL. In some complex scenarios, this is actually almost the only productive way to work with SOAP. Marshalling and unmarshalling between objects and serialized XML is rather heavy and adds to the heavyness of SOAP.

Feedback/Patterns on creating a protocol adaptor

I have a backend system that currently returns a domain object. I want to build a REST & SOAP front end using mule. To create responses that are REST or SOAP based, and ensure that the backend is protocol agnostic, I am thinking about creating two protocol adapters (REST and SOAP). It is unclear to me if it is better to create a mule translator for the service, which understands the protocol and delegates to generate an appropriate response, or just create two class interfaces (using JAX-RS/JAX-WS annotations) to the service.
I'm not finding much literature, best practices, or design patterns on the subject. Can someone point me to some useful material or provide their opinions?
Writing transformers for message formatting would be a waste of time.
You should write service classes with JAX-RS/JAX-WS annotations, transports exist for both.
For a REST service you can use the Jersey transport:
http://www.mulesoft.org/documentation/display/JERSEY/User's+Guide
For SOAP service you can use the CXF transport (also supports JAX-RS):
http://www.mulesoft.org/documentation/display/CXF/Building+a+web+service
Note: You don't need mule for this at all, both Jersey and CXF run in a servlet container like tomcat.

Categories

Resources