Showing posts with label Web Service. Show all posts
Showing posts with label Web Service. Show all posts

Saturday, June 1, 2019

Example with React, Redux and Axios API

In previous article we have seen a simple react with redux example. In this article we will add Axios api to it. Axios is used for Promise based HTTP client for the browser and node.js. To know more about axios package, click here.

In this example we will get input name from user and hit a hello world rest endpoint. For running this example you may need to install below packages -
  • redux
  • react-redux
  • axios
  • redux-thunk


App.js

Tuesday, July 29, 2014

JAXB marshalling and unmarshalling of Hashtable

It is not possible to Marshall or Unmarshall a Hashtable directly however you can achieve it using a Holder class (wrapper class). With the help of Holder class you can get the default XML format as shown below -

Default XML format -
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

However if you need to customize the XML format like the one shown below you may need to use XmlAdapter.

<colorMap>
    <colors>
        <item code="0000FF" color="blue"/>
        <item code="00FF00" color="green"/>
        <item code="FF0000" color="red"/>
    </colors>
</colorMap>

Method 1 - Using Wrapper class

Output -
--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

--Unmarshalling--
{blue=0000FF, green=00FF00, red=FF0000}


Java - JAX-WS Jar files for SOAP web services

If you are writing a SOAP based webservice you may need to add below JAR files in your applications classpath

Error: java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener
Add: jaxws-rt.jar

Error: java.lang.NoClassDefFoundError: com/sun/istack/localization/Localizable
Add: jaxb-core.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/stream/buffer/XMLStreamBuffer
Add: streambuffer.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext
Add: jaxb-impl.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/ws/policy/PolicyException
Add: policy.jar

Error: java.lang.NoClassDefFoundError: org/jvnet/staxex/XMLStreamReaderEx
Add: stax-ex.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/gmbal/ManagedObjectManager
Add: gmbal-api-only.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/external/amx/AMXGlassfish
Add: management-api.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/ha/store/api/BackingStoreException
Add: ha-api.jar

You can download latest version of JAR file (E.g. jaxws-ri-2.2.8.zip ) from https://jax-ws.java.net/2.2.8/. For more information please see the link https://jax-ws.java.net/.

Java - JAXB marshalling and unmarshalling of ArrayList

Marshalling and unmarshalling of ArrayList using JAXB is rather simple however you cannot directly marshall ArrayList. You will need a holder class (wrapper class). It will hold the arraylist that you want to marshall or unmarshall.

Please see the self explanatory java code below -

Output -

--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
    <Value>one</Value>
    <Value>two</Value>
    <Value>three</Value>
</List>

--Unmarshalling--

[one, two, three]

Friday, January 31, 2014

Implementation of Restful Webservice with image

In Java EE 6, JAX-RS provides the functionality for Representational State Transfer (RESTful) web services. REST is well suited for basic, ad hoc integration scenarios. RESTful web services, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions. Before proceeding please read this article, Developing RESTful Web Services with JAX-RS.

Also please have look at the tutorial about "JAXB marshalling and unmarshalling", as it would be helpful to understand Restful Webservice.

For running this example, below mentioned particulars are used.
  • Tomcat 7.0.11
  • JDK 1.5
  • MyEclipse 8.6.1
1. Create a new Web Service Project, File -> New -> Web Service Project


Tuesday, July 24, 2012

How to use Apache Axis2 for consuming a web service?

About the example:


In this example we will see how to use rather consume a web service using Apache Axis2. We have a Hello World web service which is already published, it accepts a String name and prints Hello name in return. To know more about how to publish a web service, click here.

Below are the steps which shows how to consume a web service using Apache Axis2

1. Download latest version of axis2 files from http://axis.apache.org/axis2/java/core/. (i.e. axis2-1.6.2-bin.zip).


2. Extract the folder (i.e. axis2-1.6.2) in your desire location. Lets assume as C:\axis2-1.6.2.

3. Add enviroment variable AXIS2_HOME and set the value "C:\axis2-1.6.2". Also make sure that JAVA_HOME is set in enviroment variables.

4. Now you can use wsdl2java.bat file from folder "C:\axis2-1.6.2\bin" to download web service stubs. Use command prompt and go to folder C:\axis2-1.6.2\bin and run below command.

   wsdl2java.bat -uri http://localhost:8080/SimpleWebservice/hellows?wsdl

   Note : Provide your wsdl URL in above command, you can also use XSD file instead of wsdl URL. Here we have already published a hello world web services. To know how to publish a simple hello world web service, click here.

   Please see the screen shot below.


Tuesday, April 24, 2012

Step by step guide to consume a web service

Consuming a web service is very easy all you need to have a WSDL url and an IDE i.e Eclipse for creating web service client. Provide WSDL url and download the stubs. Below mentioned files are automatically generated by WSDL.

Lets create a simple web service client.

Step by step process to create web service client. Please see the screen shots below.





Saturday, April 21, 2012

Step by step guide to publish a simple Hello World web service using tomcat

For running this example, below mentioned particulars are used.
  • Tomcat 7.0.11
  • JDK 1.6.0_21
  • MyEclipse 8.6

In this example, We have an interface HelloWS which contains a abstract method sayHello() which is to be exposed and a class HelloWSImpl.java which will implement HelloWS. Please go through each of the screen shots.

Creating a New Web Service Project

























Thursday, April 12, 2012

SEVERE: WSSERVLET11: failed to parse runtime descriptor: class:

If you get below mentioned Exception,

SEVERE: WSSERVLET11: failed to parse runtime descriptor: class: Bounceable could not be found
com.sun.xml.ws.model.RuntimeModelerException: class: Bounceable could not be found
                at com.sun.xml.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1574)
                at com.sun.xml.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1558)
                at com.sun.xml.ws.server.EndpointFactory.create(EndpointFactory.java:225)
                at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:145)
                at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:569)
                at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:552)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:261)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:153)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.parseAdaptersAndCreateDelegate(WSServletContextListener.java:131)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:152)
                at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
                at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
                at java.lang.Thread.run(Thread.java:619)
Apr 9, 2012 6:06:04 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener
com.sun.xml.ws.transport.http.servlet.WSServletException: WSSERVLET11: failed to parse runtime descriptor: class: Bounceable could not be found
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.parseAdaptersAndCreateDelegate(WSServletContextListener.java:141)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:152)
                at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
                at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
                at java.lang.Thread.run(Thread.java:619)
Caused by: com.sun.xml.ws.model.RuntimeModelerException: class: Bounceable could not be found
                at com.sun.xml.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1574)
                at com.sun.xml.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1558)
                at com.sun.xml.ws.server.EndpointFactory.create(EndpointFactory.java:225)
                at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:145)
                at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:569)
                at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:552)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:261)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:153)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.parseAdaptersAndCreateDelegate(WSServletContextListener.java:131)
                ... 9 more


Possible reason could be WSServlet is not able to locate endpoint class mentioned in sun-jaxws.xml.

sun-jaxws.xml (defines web service implementation class)
${Tomcat}/webapps/SimpleWebservice/WEB-INF/sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="
http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="HelloWS"
      implementation="com.javaxp.HelloWSImpl"
      url-pattern="/hellows"/>
</endpoints>


Please see the below link.

How to Publish, Consume a simple web service using Tomcat?

SEVERE: WSSERVLET11: failed to parse runtime descriptor: A @WebService.targetNamespace must be specified on classes with no package.


If you get below mentioned exception.


SEVERE: WSSERVLET11: failed to parse runtime descriptor: A @WebService.targetNamespace must be specified on classes with no package.  Class: Ball
com.sun.xml.ws.model.RuntimeModelerException: A @WebService.targetNamespace must be specified on classes with no package.  Class: Ball
                at com.sun.xml.ws.model.RuntimeModeler.getServiceName(RuntimeModeler.java:1489)
                at com.sun.xml.ws.model.RuntimeModeler.getServiceName(RuntimeModeler.java:1459)
                at com.sun.xml.ws.server.EndpointFactory.getDefaultServiceName(EndpointFactory.java:472)
                at com.sun.xml.ws.server.EndpointFactory.getDefaultServiceName(EndpointFactory.java:461)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:236)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:153)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.parseAdaptersAndCreateDelegate(WSServletContextListener.java:131)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:152)
                at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
                at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
                at java.lang.Thread.run(Thread.java:619)
Apr 9, 2012 6:04:00 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener
com.sun.xml.ws.transport.http.servlet.WSServletException: WSSERVLET11: failed to parse runtime descriptor: A @WebService.targetNamespace must be specified on classes with no package.  Class: Ball
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.parseAdaptersAndCreateDelegate(WSServletContextListener.java:141)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:152)
                at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
                at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
                at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
                at java.lang.Thread.run(Thread.java:619)
Caused by: com.sun.xml.ws.model.RuntimeModelerException: A @WebService.targetNamespace must be specified on classes with no package.  Class: Ball
                at com.sun.xml.ws.model.RuntimeModeler.getServiceName(RuntimeModeler.java:1489)
                at com.sun.xml.ws.model.RuntimeModeler.getServiceName(RuntimeModeler.java:1459)
                at com.sun.xml.ws.server.EndpointFactory.getDefaultServiceName(EndpointFactory.java:472)
                at com.sun.xml.ws.server.EndpointFactory.getDefaultServiceName(EndpointFactory.java:461)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:236)
                at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:153)
                at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.parseAdaptersAndCreateDelegate(WSServletContextListener.java:131)
                ... 9 more

You shoud check for package name for Java Classes and Interfaces within the Web service project. Default package is not allowed for them. You should explicitly add them to some package.

Please see belwo link.

How to Publish, Consume a simple web service using Tomcat?

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at


If You get below mentioned exception, possible reason could be -

1. Your WSDL url is not correct
2. Your WSDL url is not responding.


Check by hiting the URL into your browser, it should appear as shown below.




Exception :

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:8080/HelloWS?wsdl. It failed with:
                Connection refused: connect.
                at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:162)
                at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:144)
                at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:263)
                at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:226)
                at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:174)
                at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:104)
                at javax.xml.ws.Service.(Service.java:56)
                at simple.SimpleServiceImplService.(SimpleServiceImplService.java:53)
                at simple.TestClient.main(TestClient.java:17)
Caused by: java.net.ConnectException: Connection refused: connect
                at java.net.PlainSocketImpl.socketConnect(Native Method)
                at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
                at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
                at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
                at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
                at java.net.Socket.connect(Socket.java:529)
                at java.net.Socket.connect(Socket.java:478)
                at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
                at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
                at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
                at sun.net.www.http.HttpClient.(HttpClient.java:233)
                at sun.net.www.http.HttpClient.New(HttpClient.java:306)
                at sun.net.www.http.HttpClient.New(HttpClient.java:323)
                at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
                at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
                at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
                at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
                at java.net.URL.openStream(URL.java:1010)
                at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:805)
                at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:262)
                at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:129)
                ... 7 more

How to Publish, Consume a simple web service using Tomcat?

 

How to Publish, Consume a simple web service using Tomcat


Web services are platform independent hence it is used to communicate between different languages over http protocol. Web Services can convert your application into a Web-application, which can publish its function or message to the rest of the world. The basic Web Services platform is XML and HTTP.

Publishing and Consuming a Helloworld web service is very easy, all you need to have is a JDK (1.6 or higher),a Tomcat server and a IDE (e.g. Eclipse) to create a web service client you need to download stub. In this article we will see how to publish a simple JAX-WS web services using Tomcat servlet container. Also we will see how to create a simple web serive client. i.e. How to consume web service?

How to publish simple web service?

For running this example, below mentioned particulars are used.
  • Tomcat 7.0.11
  • JDK 1.6.0_21
  • MyEclipse 8.6
In this example, We have an interface HelloWS which contains a abstract method sayHello() which is to be exposed and a class HelloWSImpl.java which will implement HelloWS.

Please go through each of the screen shots.

Creating a New Web Service Project


Add project name, lets say SimpleWebservice



/* HelloWS.java */

package com.javaxp;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWS {

public String sayHello(String name);
}

/* HelloWSImpl.java */

package com.javaxp;

import javax.jws.WebService;

@WebService(endpointInterface="com.javaxp.HelloWS")
public class HelloWSImpl implements HelloWS {

public String sayHello(String name) {

return "Hello "+name+"!! this is your first web service.";
}
}

web.xml (defines WSServletContextListener)
${Tomcat}/webapps/SimpleWebservice/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
                xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <listener>
        <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
</web-app>

sun-jaxws.xml (defines web service implementation class)
${Tomcat}/webapps/SimpleWebservice/WEB-INF/sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="HelloWS"
      implementation="com.javaxp.HelloWSImpl"
      url-pattern="/hellows"/>
</endpoints>


If JAXB is not bundled with your tomcat you have to add below mentioned JAR files in ${Tomcat}/lib.
Download JAR file from https://jax-ws.java.net/

  • gmbal-api-only.jar
  • ha-api.jar
  • jaxb-impl.jar
  • jaxws-api.jar
  • jaxws-rt.jar
  • jaxb-api.jar
  • management-api.jar
  • policy.jar
  • stax-ex.jar
  • streambuffer.jar
  • jaxb-core.jar