Sunday, May 22, 2016

Hosting a SOAP Web Service Java


Step 1
Create a dynamic web project in eclipse
Create an endpoint interface
package org.myProject.passwordManager;
 import javax.jws.WebMethod;
 import javax.jws.WebService;
 @WebService public interface PasswordManagerWS {
 @WebMethod public abstract String getPassword(String appId);
 }
Create an implementation of Web Service
package org.myProject.passwordManager;
 import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding; @WebService(endpointInterface="org.myProject.passwordManager.PasswordManagerWS", serviceName="PasswordManagerWS")
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT)
 public class PasswordManagerWSImpl implements PasswordManagerWS{
 @WebMethod public String getPassword(@WebParam(name="appId")String appId) { System.out.println("test ws"); return "test";
 }
 }

SOAP Web Services Java


Web Services are generally used for machine to machine communication.SOAP is a protocol for communication.

Topics

Hosting a SOAP Web Service

Monday, January 18, 2016

Proxy Class in java


What is a Proxy Class?

Proxy is a class in java which is used to create a dynamic class.

Why do we use proxy class?

links

description

Web Services Interview Questions


Web services is vast topic , lets start with creating a simple web service.I have tried explaining the concepts using a sample code ,In any point if you are unable to understand please go through the documentation links .
  • Create a sample web service as below
package org.myProject;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface TestWS {
@WebMethod
public abstract String getMethod(String param);
}
TestWS is a web service
  • Implement the web service
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(endpointInterface="org.myProject.TestWS",serviceName="TestWS")
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT)
public class TestWSImpl implements TestWS{
@WebMethod
public String getMethod(@WebParam(name="param")String param) {
System.out.println("test ws");
return "test";
}
}
In addition to implementing the method getMethod , a service name is also specified for the web service.
We have linked TestWSImpl class to TestWS using annotation param endpointInterface="org.myProject.TestWS" . Now,as we know, web service is a communication between two machines via xml messages , so an endpoint is reference to which messages are addressed.
  • Host the web service
There are several ways to host your web service.I choose to host it through jboss EAP 6.The web service need to be packaged in war file and deployed to jboss .
Service name is to be mapped in web.xml as below:
<servlet>
<servlet-name>PasswordManagerWSImpl</servlet-name>
<servlet-class>org.myProject.passwordManager.PasswordManagerWSImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PasswordManagerWSImpl</servlet-name>
<url-pattern>/TestWS</url-pattern>
</servlet-mapping>

Once the server is up and running , you can verify whether the web service is hosted properly
Go the following url
http://localhost:8089/TestWebService/TestWS?wsdl
Replace TestWebService with the name of WAR file deployed.
If on hitting the link you are able to get the WSDL then the web service is successfully hosted.
WSDL has the details of the web service which is used by the client to communicate to the web service.
  • Client Interaction with the web service
QName qName=new QName("http://myProject.org/","TestWS");
URL url=new URL("http://localhost:8089/TestWebService/TestWS?wsdl");
Service service=Service.create(url,qName);
TestWS webSer=service.getPort(TestWS.class);
String test=webSer.getMethod("test");
A QName object is created by passing namespaceURI as targetNamespace and localPost as service name from WSDL.
URL object is created by passing url of WSDL.
References:

Struts 1 Interview Question

Wednesday, September 16, 2015

Spring MVC



Spring MVC has below major components :

DispacherServlet : it is a common entry point of all http request . It forwards the request to the controller .Spring MVC follows front controller design pattern in which there is one controller which responds to all the requests.

HandlerMapping : it determines the controller to be called from request url .

Controller : it services the request and sets up data to the model and pass it to View.

ViewResolver : it determines the view to which request is to be transferred.

Lets see how can make use for the above Spring components in a web application.

In web.xml file  make an entry for dispatcher servlet.
<servlet-name>spring-web</servlet-name>
<servlet-class>
                    org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

Then map your application URL to the disptcher servlet.

<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/urlOfApp</url-pattern>
</servlet-mapping>



Create a file spring-web-servlet.xml and place it in WEB-INF with the following content.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">



<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:annotation-driven />

</beans>


The naming convention of this file is [servlet name]-servlet.xml  , if we need to put a custom name or place it in custom path you need to configure the  listener class .We can also specify multiple application context xml  using the attribute. This is usually done to separate the web application context with the spring root application context. The web application context can be placed in WEB-INF folder which will be created when dispatcher servlet is loaded . The root application context can be created when the Context Loader Listener is loaded .



<listener>
<listener-class>org.spring.framework.web.context.ContextLoaderListener</listener-class>

</listener>
context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/common-config.xml</param-value>
</context-param>

If we need to have multiple web application configuration in different XML files we can also configure that.



<servlet>
    <servlet-name>spring-web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/main-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>spring-web2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/sub.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


To use annotations to configure beans instead of xml configuration we need to add the following line of code in Spring configuration xml.

<context:component-scan base-package="org.myProject.question" />

To create a controller class use annotation @Controller:
Controller :

@Controller
public class SampleController
{
@RequestMapping(value = "/controllerUrl")
public String performAction(ModelMap requestResponse)
{
String viewName =" home";
requestResponse.put("message","Welcome to Home page");
return viewName;
}
}


Controller returns the view name , View page is a jsp. Which has to be placed in the path specified for the ViewResolver bean , if you look into the spring-web-servlet.xml we have specified the path as WEB-INF/views/jsp using prefix and suffix attributes .


<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Home Page</title>
   </head>

   <body>
      <h2>${message}</h2>
   </body>
</html>


So let's try to build a web application with a home page .




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>My App </display-name>
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>
                    org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
</web-app>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">



<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:annotation-driven />

</beans>
@Controller
public class SampleController
{
@RequestMapping(value = "/home")
public String performAction(ModelMap requestResponse)
{
String viewName =" home";
requestResponse.put("message","Welcome to Home page");
return viewName;
}
}
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>
 
   <body>
      <h2>${message}</h2>
   </body>
</html>

Let's try to build a restful web service using Spring MVC framework .
The following example will create a web service and list all the products .

Rest Controller :


@Controller
public class ProductController
{
@RequestMapping(value = "/products")
public @ResponseBody ProductListVO getAllProducts()
{
ProductListVO products = new ProductListVO();
//service call to get the products
return products;
}


End




ec2-user@ec2 Permission denied