How to call a JSON Rest web service with Tomee 1.6 - java

My application is running on a Tomee plus 1.6 JEE server.
I need to call a RS WS that will return an object formatted as a JSon string.
I can do that with an openConnection("url"), and a manual parsing of the response. But I think there is a much higher way to do that in a JEE environment.
The problem is that I found many tutorials about how to write a Restful WS, but very few about how to invoke them. Moreover, eachtime, there are several libraries to add to the Tomee server (Jackson, ...).
My questions are :
is there any standard way to invoque a WS with no library to add ? I think there must be one because Tomee plus is supposed to be a full JEE server.
how to do that ?
Thanks for your help.

If you are not on tomee 7 but still tomee 1 you can use CXF WebClient to do that. TomEE 7 would let you use standard JAX-RS 2 client (part of EE 7) but was not in TomEE 1 (EE 6)

There are plenty of options to consume REST applications in Java nowadays. The current standard is the JAX-RS Client API, released in JAX-RS 2.0, and it's part of the Java EE 7 API.
Important: Once your are using Apache TomEE 1.6.0, which is Java EE 6 certified, the JAX-RS Client API won't be available for you. Keep reading.
JAX-RS Client API
The JAX-RS Client API (javax.ws.rs.client package), defined in the JSR 339 and released as part of the Java EE 7 API, is the standard way to consume REST web services in Java. Besides others, this specification is implemented by Jersey, RESTEasy and Apache CXF.
Vendor specific proxy frameworks
Both Jersey, RESTEasy and Apache CXF also provide a proxy framework API. Those APIs are vendor specific and are not part of the JAX-RS Client API.
The basic idea is you can attach the standard JAX-RS annotations to an interface, and then implement that interface by a resource class on the server side while reusing the same interface on the client side by dynamically generating an implementation of that using java.lang.reflect.Proxy calling the right low-level client API methods.
For more details check the following:
Jersey proxy-based client API
RESTEasy proxy-based client API
Apache CXF proxy-based client API
Other resources
Here are some other options that you could consider as alternative to the JAX-RS Client API:
Apache CXF WebClient API †
Spring RestTemplate
OkHttp
Retrofit
Netflix Feign
† Apache CXF WebClient API: It's shipped with the Apache TomEE 1.6.0. In the cxf-rt-frontend-jaxrs.jar you are going to find the WebClient class, which is the entry point to the client API.

Thank you very much for your help.
Finally, I decided to use the standard JAX-RS API, but as it was not released yet in Tomee 1.6, I had to add a jar file in the lib directory of the server :
javax.json-1.0.4.jar, which I downloaded from Oracle web site.
To compile my project, I had to use an other jar file : javax.json-api-1.0.4.jar

Since you have mentioned that you are using TomEE plus 1.6, you already have JAX-RS client (Ref: Apache TomEE) which can be used to access REST WS.
The javax.ws.rs.client can be used to invoke the REST service and receive the response. More detailed tutorial can be found on Oracle Site.
EDIT: Missed the version number part. The API is available in 1.7.

Related

Is it possible to configure a Java web app to run with Apache Wink Server and JAX-RS 2.0?

I switched my application from using a Jersey Server to using an Apache Wink Server. Now, when I make CRUD operations to my API, I get the following error:
javax.servlet.ServletException: java.lang.AbstractMethodError: javax.ws.rs.core.Response.getStatusInfo()
I am sure this is happening because my application requires the use of JAX-RS 2.0, but Apache Wink is only JAX-RS 1.1 compliant.
I know that Apache CFX is JAX-RS 2.0 compliant, but I need to use Apache Wink to make the server responses align with a Liberty server (which uses Apache Wink).
So, is it possible to get Apache Wink + JAX-RS 2.0 working together on the same Java web app? And if so, please help me!

How to decide on what framework to use in Java Web Service?(SOAP)

I am fairly new in Java EE web service. Right now we have a project to create an API web service that connects to a database and do some retrieve and write functions.
I've heard about the following:
Axis
Struts
Spring
Can someone please enlighten me as to what framework is applicable for the said project? I've tried Google of course but I need opinion on people who have experience on the said framework.
BTW we are going to create a SOAP web service. Additional tips are also appreciated.
Note that Java has the JAX-WS API which is a technology for building web services and clients that communicate using XML. In JAX-WS, a web service operation invocation is represented by an XML-based protocol, such as SOAP.
From the frameworks you mentioned, Spring brings SpringWS which you can use to build a SOAP web service.
However i don't know about Struts 2 core api offering any SOAP capabilities.
But it can be extend with other plugins that handle SOAP.
Axis (use the latest, Axis 2) is a good choice for SOAP. It is a Web Services / SOAP / WSDL engine. It also has some support for the Spring Framework.
There is also Apache CXF. It is the most widely used Web Services Standard Now; Improvement over AXIS2, which is now gradually being replaced by Apache CXF
If you need help deciding between them, read this comparison (Apache CXF vs. Apache AXIS vs. Spring WS) for the pros and cons.
There's also this great answer about cxf and axis 2.

Difference between JAX-WS, Axis2 and CXF

What is the difference between:
JAX-WS
Axis2
CXF
All three can be used to create webservices in Java.
As of I know JAX-WS is a specification and Axis2 and CXF are implementations, but Java 1.6 has implementation of JAX-WS if I am not wrong.
So one can use Java 1.6 to develop JAX-WS web services without using Axis2 or CXF? Then what is the use of Axis2, CXF?
The JAX-WS implementation built into the JDK really is just the basic soap stuff. If you need any of the more complex WS-* things like WS-Security, WS-RM, WS-Policy, etc..., you need to use one of the alternatives like CXF or Metro or Axis2. It can also depend on what you are trying to integrate with. For example, CXF has top notch Spring support as well as very good OSGi support.
CXF also has other things besides just JAX-WS. It has a compliant JAX-RS implementation as well and supports exposing services as both REST and SOAP very well. Has a W3C compliant SOAP/JMS implementation if that type of things is required. Basically, lots of stuff not available from the in-jdk JAX-WS impl.
Also see:
Difference between Apache CXF and Axis
In short.
WSDL WS-* are language-agnostic.
JAX-WS are Java standard to build web service.
Apache CXF and Apache Axis 2 are two implementations of JAX-WS. They also offer JAX-RS implementations so that you can build Restful services.
CXF has better integration with Spring, and Camel(camel-cxf). And Axis 2 seems not have a active release.
I found with CXF - integration with Spring is very easy. Moreover, It provides various features like:
Customization of Logging features
Inbound and Outbound interceptor
Application Level security
Easy Exception handling using custom Fault.
For more detail, if you want, please checkout this link:
http://predic8.com/axis2-cxf-jax-ws-comparison.htm
http://www.ibm.com/developerworks/java/library/j-jws11/
And, I read above links, its preety helpful for me. I hope it works for u too.
Thanks !
Web Service organization reles some guidelines i.e BP(Basic Profile) 1.0 and BP(Basic Profile) 1.1.
Base on the Guidelines All Language(Java/.Net) people release Specification with Default implementation
In java Base on BP 1.0 Specification Is JAX-RPC And 1.0 Specification Is JAX-WS
JAX-WS Default implementation is RI(Reference Implementation)
Base on the requirement/Choice we can change the implementation(RI/AXIS-2/CXF)
When we are using java 1.6 that case in JDK already available so not required any other jar. If you want to use different implementation that case required particular implememtation jar.
Axis-2 and CXF come from Apache

Is Jersey just a framework for developing RESTful web services in Java?

The way I understand it is Java EE 6 includes the classes for java.ws.rs (JAX-RS) which are defined in the JSR 311 spec document. But I don't know why you would use Jersey or Apache CXF if the base classes are already built into Java EE 6. Can you not create a RESTful web service with those classes alone? Are Jersey, Apache CXF, etc just frameworks to make development of REST-based web services easier?
why you would use Jersey or Apache CXF if the base classes are already built into Java EE 6.
Can you not create a RESTful web service with those classes alone?
Java EE only defines standards, those classes are the standard API, but there is no implementation behind them. Jersey and CXF are competing implementations of the standard.
However, if you have a server that claims to support Java EE 6, it will have to contain an implementation for every API that's in the standard. For example, Glassfish includes Jersey, so you don't have to add it explicitly.
JAX-RS is just a specification. In order to use JAX-RS, you need an implementation of the spec.
Jersey is a JAX-RS implementation. Specifically, it is the reference implementation.

Simply consuming a web service in Java

I have a very simple SOAP web service that I need to consume from a Java client. What is the easiest way to accomplish this without using any third party libraries? A requirement is that the host and port is read from the web.xml before every call to the ws.
I can recommend you CXF library. Using it you will have several options for calling web services:
Use dynamic proxy for calling (don't need to make Java stubs using wsdl2java).
DynamicClientFactory dcf = DynamicClientFactory.newInstance();
Client client = dcf.createClient("http://admin:password#localhost:8080"+
"/services/MyService?wsdl");
Object[] a = client.invoke("test", "");
System.out.println(a);
Using Java stub generated from WSDL, using wsdl2java.
If your server was created using CXF you can reuse your interface code directly (instead of using wsdl2java on the WSDL which was created from your interface!)
For both #2 and #3, the following code exemplifies the CXF usage:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://admin:password#localhost:8080/services/MyService");
factory.setServiceClass(ITest.class);
ITest client = (ITest) factory.create();
client.test();
Depending on which version of JAVA you're using, some of the JAX-WS is built into it. JDK 6 has Java's JAX-WS standard implementation and you could just use it.
See the following:
JAX-WS 2.1 and JAXB 2.1 is available in JDK 6 Update 4 release
Getting Started with JAX-WS Web Services (tutorial to use the JDK built-in JAX-WS for deploying and consuming a web service)
If you can relax your "no 3rd party libraries" requirement, and you have a WSDL for the web service then Axis makes it really easy. Just compile the WSDL using wsdl2java, and you can use the generated Java classes to consume the web service.
Without using any third party libraries? Get to know the SOAP standard really well and learn to love SAX.
If you can't love SAX, then lax your no-third-party-libs requirement and use StAX (with woodstox) instead.
This approach might be the "easiest" (considering the no-third-party-libs requirement) but I don't think it will be easy.

Categories

Resources