0

I have searched the net and found few solution,but still i am facing the same problem.

I am trying to create a web application with Angularjs as frontend end spring rest as back end. I am able to access the url resource through $http.post methos, but while the binding of data to pojo doesnt happen. The values are always null.

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-  app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>login</display-name>
<servlet>
    <servlet-name>springws</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springws</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

My dispatcher servlet configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.junaid.spring.webservice" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

 <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

 <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

 <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <!-- Drop and re-create the database schema on startup -->
            <prop key="hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>   
</beans>

Angualrjs controller file

login.controller('RegisterController',['$scope','userFactory', function($scope, userFactory) {

$scope.user;

$scope.saveUser = function(){
    userFactory.saveUser($scope.user);
};
}]);

Angularjs factory js file

angular.module('login')
.factory('userFactory' , ['$http', function($http){

var userFactory= {};

userFactory.authenticate = function(user){
    console.log(user.name);
    console.log(user.password);
};

userFactory.saveUser = function(user){
    console.log(user.name);
    console.log(user.password);
    console.log(user.phone);
    console.log(user.email);
     $http.post('rest/register', user).success(console.log("registered"));
};

userFactory.forgotPassword = function(user){
    console.log(user.name);
};

return userFactory;
}]);

Jsp page invokes userFactory.saveUser() function which in turns call my rest service,

my controller class

@Controller
public class UserController {

@RequestMapping(value = "/authenticate", method = RequestMethod.GET)
public  @ResponseBody String getState(UserData ud) {
    System.out.println(ud.getPassword());
    return "true";
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public  @ResponseBody String registerUser(Users ud) {
    System.out.println(ud.getPassword());
    return "true";
}
}

The println statements always prints null.

Can anyone tell me where i am going wrong.

1 Answer 1

1

You need the @RequestBody in your getState and registerUser methods before the Argument. Furthermore getState must be a POST Method.

@Controller
public class UserController {

@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public  @ResponseBody String getState(@RequestBody UserData ud) {
    System.out.println(ud.getPassword());
    return "true";
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public  @ResponseBody String registerUser(@RequestBody Users ud) {
    System.out.println(ud.getPassword());
    return "true";
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.