Sunday, September 9, 2012

Spring MVC with Ajax

Spring MVC with AJAX implementation using Jackson API and Jquery

The following jar files needed for this ,

commons-logging-1.1.1.jar
jackson-core-asl-1.4.0150249.jar
jackson-mapper-asl-1.4.3.jar

jstl-1.2.jar
spring-asm-3.0.3.RELEASE.jar
spring-beans-3.0.3.RELEASE.jar
spring-context-3.0.3.RELEASE.jar
spring-context-support-3.0.3.RELEASE.jar
spring-core-3.0.3.RELEASE.jar
spring-expression-3.0.3.RELEASE.jar
spring-web-3.0.3.RELEASE.jar
spring-webmvc-3.0.3.RELEASE.jar


Ajax implementation is based on 
 jackson-core-asl-1.4.0150249.jar and jackson-mapper-asl-1.4.3.jar

1) Added this jars in your classpath.

2) Add the following configuration in your context file,

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
          <list>
            <ref bean="
jsonConverter"/>
          </list>
    </property>

</bean>

3) Controller Class(EmployeeController.java)

package com.jijo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EmployeeController {

     @RequestMapping(value = "/SaveEmploye.html", method = RequestMethod.POST)
        public @ResponseBody String saveEmployee(@RequestBody Employe employe) {
           if(employe != null){
               System.out.println("*****DATA IS NOT NULL*****: "+employe);
               for(Address address :employe.getAddresses()){
                   System.out.println("Employee Name:"+employe.getFirstName()+" \nHouseName:"+address.getHouseName());
               }
           }
           else{
               System.out.println("employe is null");
           }
            return "done";
        }
}



4) Employe.java

package com.jijo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Employe  implements Serializable{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String employeeId;
    private String firstName;
    private String lastName;
    private String department;
    private List<Address> addresses = new ArrayList<Address>();
    public String getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public List<Address> getAddresses() {
        return addresses;
    }
    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }
   
}




5) Addess.java
 


package com.jijo;

import java.io.Serializable;

public class Address implements Serializable{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String houseName;
    private String place;
    private String district;
    private String state;
    private String country;
    private String postalCode;

    public String getHouseName() {
        return houseName;
    }

    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }
}



6) Javascript function




function saveEmploye(){
var addresses = [];
var employee = {"employeeId":"10","firstName":"John","lastName":"Smith","department":"IT"};
var address = 

{"houseName":"HouseNo-10",
"place":"PLACE1",
"district":"district1","state":"state1"};
addresses.push(address);
employee.addresses=addresses;

$.ajax({
    type: "post",
    url: "SaveEmploye.html", //your url
    contentType: "application/json",
    data: JSON.stringify(employee),
    success: function(result) {
        alert('Employee saved successfully');
    },
    error: function(){
        alert('
Error occured');
    }
});
}
 

 




Saturday, March 24, 2012

Authentication in Jboss webservice client

URL url = null;
url = new URL("file:/c:/test/test.wsdl");


    PRODGenericDataAccessSoap soap = super.getPort(PRODGenericDataAccessSoap, PRODGenericDataAccessSoap.class);
        BindingProvider bp = (BindingProvider)soap;
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "jijo");

//username is jijo
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "jijo"); 

//passowrd is jijo
        return soap; 


16:12:49,843 WARN  [org.apache.cxf.phase.PhaseInterceptorChain] Interceptor for {http://www.prodml.org/api/200/genericDataAccess}PROD_GenericDataAccess#{http://www.prodml.org/api/200/genericDataAccess}GetData has thrown exception, unwinding now: java.lang.RuntimeException: Can't find input stream in message
    at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:116) [:2.3.1]
    at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:60) [:2.3.1]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255) [:2.3.1]
    at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:755) [:2.3.1]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2330) [:2.3.1]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:2192) [:2.3.1]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:2036) [:2.3.1]
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56) [:2.3.1]
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:696) [:2.3.1]
    at 16:12:51,765 ERROR [STDERR] javax.xml.ws.soap.SOAPFaultException: Can't find input stream in message