java - Spring POST 不返回有效数字

标签 java javascript spring angularjs

我对 spring 非常陌生,但我设法从我的 javascript 中想出了这段代码

$scope.addMe = function(){
        var params = {
            date :111111,
            username: 'thisGuy',
            notetext: 'this is a new note that was just added'
        };
        $http.post(afHttp.baseUrl + "/blah/" + $scope.dog.id, {params: params})
            .success(function(data) {
            });
    }

在 elclipe 中我有

@POST
@Path("/{id}")
public void getAddMssg(@PathParam("id") int id, @FormParam("date") int date, @FormParam("username") String username, @FormParam("notetext") String notetext) {
    System.out.println("date:      "+date);
    System.out.println("username:   "+ username);
    System.out.println("notetext:   "+ notetext);
    System.out.println("id:   "+ id);

}

当我在 eclipse 中查看我的控制台日志时,它说

date:   0
username:   null
notetext:   null
id:   123124

我做错了什么?我已经为此工作了一段时间但无济于事。有人可以帮忙吗?

哦,对不起,我忘了补充,我正在使用 angularjs。

最佳答案

将参数传递给该参数,而不是 {params:params}。 此外,将 Content-Type header 添加为第三个参数:

$http.post(afHttp.baseUrl + "/blah/" + $scope.dog.id, params, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded'}})

在服务器端,使用@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 进行@POST:

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)

MediaType 的完全限定名称是 javax.ws.rs.core.MediaType,以防您需要导入它。

关于java - Spring POST 不返回有效数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19608508/

相关文章:

java - 如何在 Vista 中搜索文件(例如 Java 文件)?

java - 从 Android 应用程序比较其他表的表中删除一行

javascript - 如何找到unicode组合?

javascript - 如何以像素为单位降低 VH 的值?

java - 如何使用字符串参数向 spring boot rest 端点添加验证?

java - org.springframework.ws.soap.client.SoapFaultClientException : Server was unable to process request. ---> 超时已过期

java - 在 Swing 中右键单击焦点

Java 接口(interface)约定 getter 和 setter

javascript - MongoDB/Mongoose - 仅当特定字段唯一时才将对象添加到对象数组

java - Spring Boot 测试服务(使用带有 JDBCTemplate 的存储库)