OfferTransform Your Career with Expert-Led IT Training. Flat discounts active!Explore Now
OnlineITGuru Logo
AI & Machine Learning

REST Web Services

Last updated on Jun 15, 2020

Copy Link:
REST Web Services
REST Web Services:
Java Web Services

Java web administrations instructional exercise gives ideas and cases of two primary java web administrations programming interface. REST of JAX-WS and REST of JAX-RS. The java web benefit application can be gotten to by other programming dialects, for example, .Net and PHP.JAX-WS instructional exercise is gives ideas and cases of JAX-WS API. This JAX-WS instructional exercise is intended for amateurs and experts.

Java web benefit application performs correspondence through WSDL (Web Services Description Language). There are two essential for java web benefit source code: SOAP and RESTful.

Java Web Services API There are two essential API's portrayed by Java for making web advantage applications since JavaEE6. 1) JAX-WS: for SOAP web organizations. The two ways to deal with form JAX-WS application code: by RPC style and Document style. 2) JAX-RS: for RESTful web organizations. There are generally 2 utilizations at this moment being utilized for making JAX-RS application: Jersey and REST simple. JAX-WS is provides concepts and examples of JAX-WS API. This JAX-WS tutorial is designed for beginners and professionals.

JAVA Tutorial Video

[embed]https://www.youtube.com/watch?v=UbpbqqIw_3Q&t=1725s[/embed] RPC style:
  • RPC style web services use method name and parameters to generate XML structure.
  • The generated WSDL is difficult to be validated against schema.
  • In RPC style, SOAP message is sent as many elements.
  • RPC style message is tightly coupled.
  • In RPC style, SOAP message keeps the operation name.
  • In RPC style, parameters are sent as discrete values.
WSDL file: In WSDL file, it doesn't specify the type details. JAX-WS Example RPC Style:

Making JAX-WS illustration is a simple errand since it requires no additional design settings.JAX-WS API is inbuilt in JDK, so you don't have to stack any additional jug petition for it.

There are made 4 records for hi world JAX-WS illustration

HelloWorld.java HelloWorldImpl.java Publisher.java HelloWorldClient.java The initial 3 documents are made for server side and 1 application for customer side.
JAX-WS Server Code
File: HelloWorld.java:
 package com.javaspot; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface HelloWorld{ @WebMethod String getHelloWorldAsString(String name); } File: HelloWorldImpl.java:  package com.javaspot; import javax.jws.WebService; //Service Implementation @WebService(endpointInterface = "com.javatpoint.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString(String name) { return "Hello World JAX-WS " + name; } } File: Publisher.java:  package com.javaspot; import javax.xml.ws.Endpoint; //Endpoint publisher public class HelloWorldPublisher{ public static void main(String[] args) { Endpoint.publish("http://localhost:7779/ws/hello", new HelloWorldImpl()); } } How to view generated WSDL After running the publisher code, you can see the generated WSDL file by visiting the URL http://localhost:7779/ws/hello?wsdl JAX-WS Client Code  File: HelloWorldClient.java  package com.javaspot; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class HelloWorldClient{ public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:7779/ws/hello?wsdl"); QName qname = new QName("http://javaspot.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.getHelloWorldAsString("javaspot rpc")); } }  JAX-WS Example Document Style: Like RPC style, we can make JAX-WS case in report style. To do as such, you have to change just a single line in benefit interface. You have to utilize Style.DOCUMENT for @SOAPBinding comment set up of Style.RPC. Document: HelloWorld.java @SOAPBinding(style = Style.DOCUMENT)//It is changed from RPC to DOCUMENT Presently Let's see the full case of JAX-WS in record style. There are made 4 records for hi world JAX-WS archive style case. HelloWorld.java HelloWorldImpl.java Publisher.java HelloWorldClient.java The initial 3 records are made for server side and 1 application for customer side. JAX-WS Server Code  File: HelloWorld.java:  package com.javaspot; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.DOCUMENT) public interface HelloWorld{ @WebMethod String getHelloWorldAsString(String name); } File: HelloWorldImpl.java:  package com.javaspot; import javax.jws.WebService; //Service Implementation @WebService(endpointInterface = "com.javaspot.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString(String name) { return "Hello World JAX-WS " + name; } } File: Publisher.java:  package com.javaspot; import javax.xml.ws.Endpoint; //Endpoint publisher public class HelloWorldPublisher{ public static void main(String[] args) { Endpoint.publish("http://localhost:7779/ws/hello", new HelloWorldImpl()); } } Now, it will generator 2 files:
  • SayHello
  • SayHelloResponse
Paste these files in com.javaspot directory and then run the publisher class.
JAX-WS Client Code
File: HelloWorldClient.java:
package com.javaspot;
import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class HelloWorldClient{ public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:7779/ws/hello?wsdl"); QName qname = new QName("http://javaspot.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); system.out.println(hello.getHelloWorldAsString("javaspot document")); } }