Satyadev Satyadev - 6 months ago 39
AngularJS Question

Sending text area values as request to spring is always null

I have two textarea boxes like this:

<div class="form-group">
<label for="comment">Numbers:</label>
<textarea class="form-control" rows="3" ng-model="numbers" placeholder="Numbers in one per line format"></textarea>
<br>
<label for="comment">Message:</label>
<textarea class="form-control" rows="2" ng-model="message" ng-trim="true" placeholder="Enter your message here"></textarea>
</div>


I am sending the request from angular:

$scope.sendSMS = function () {
var formData = {
numbers: $scope.numbers,
message: $scope.message
};

Auth.sendSMS(formData, successAuth, function () {
console.log(JSON.stringify(formData)); //prints both values
$rootScope.error = 'Message not sent';
console.log($rootScope.error);
});
};


Auth service (http post):

return{
sendSMS: function(data, success, error){
$http.post('mvc-dispatcher/sendSMS', data).success(success).error(error);
}
}


While on front-end I am able to print both the values before sending, however on server-side the text area for message is always taking it is null and text-area for number is printing correctly:

Numbers: 123456789
message: null


I am not sure what went wrong. There is no error on server-side nor frontend

Rest Controller:

@RestController
public class smsController {

@RequestMapping(value = "/sendSMS", consumes = "application/json", headers = "content-type=application/x-www-form-urlencoded")
public void sendMessage(@RequestBody MessageBean msgBean) {

String numbers = msgBean.getNumbers();
System.out.println("Numbers: " + numbers);
String message = msgBean.getMessages();
System.out.println("message: " + message);
}
}


Dispatcher-servlet:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.spring.rest.controllers" />
<mvc:annotation-driven />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>


Jars used:

jackson-annotations-2.7.3.jar
jackson-databind-2.7.3.jar
jackson-core-2.7.3.jar
Spring Framework release 4.2.2


Message Bean:

public class MessageBean {

private String numbers;
private String messages;

public String getNumbers() {
return numbers;
}

public void setNumbers(String numbers) {
this.numbers = numbers;
}

public String getMessages() {
return messages;
}

public void setMessages(String messages) {
this.messages = messages;
}
}

Answer

You've got a wrong parameter name, try this;)

var formData = {
    numbers: $scope.numbers,
    message: $scope.message
};

Edited:
Now we are clear, you declared a field messages in MessageBean, but you tried to give it a parameter named message, message will not be matched to messages when Spring do convertering things, so you should try this;)

var formData = {
    numbers: $scope.numbers,
    messages: $scope.message
};