将文本区域值作为请求发送到spring始终为null

时间:2016-05-22 18:41:15

标签: angularjs spring spring-mvc spring-restcontroller spring-rest

我有两个像这样的textarea框:

<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>

我从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);
    });
};

验证服务(http post):

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

虽然在前端我能够在发送之前打印这两个值,但是在服务器端,消息的文本区域始终为空,并且数字的文本区域正确打印:

Numbers: 123456789
message: null

我不确定出了什么问题。服务器端和前端没有错误

休息控制器:

@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);
    }
}

分配器一小服务程序:

<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>

使用的罐子:

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;
    }   
}

1 个答案:

答案 0 :(得分:1)

您的参数名称错误,请尝试此操作;)

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

<强>编辑:
现在我们很清楚,您在messages中声明了一个字段MessageBean,但是您试图为其指定一个名为message的参数,message将不会与messages匹配当Spring转换东西时,你应该试试这个;)

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