【发布时间】:2015-05-25 11:13:51
【问题描述】:
所以我正在构建一个电子邮件客户端,它可以对 Google API 进行授权的 RESTful 调用(OAuth 2 和 JAX-RS)。
我已成功导入电子邮件和联系人。我现在想发送一封电子邮件。
我有一个控制器调用服务(成功)
app.controller('ComposeController', function($scope, $cookies, ComposeService) {
//Some test data
var email = $cookies.email;
$scope.to = "********@yahoo.com";
$scope.subject = "test";
$scope.body = "test";
$scope.compose = function() {
//Create an email object
$scope.newEmail = {
'to' : $scope.to,
'from' : email,
'subject' : $scope.subject,
'body' : $scope.body
};
//Parse to JSON
var emailJSON = angular.toJson($scope.newEmail);
//Make call to ComposeService
var response = ComposeService.compose(emailJSON).success(function(jsonData) {
response = jsonData;
alert(response);
});
}
});
Service 然后调用 Java 类
app.factory('ComposeService', function($http) {
var response = {};
response.compose = function(newEmail) {
return $http({
method: 'POST',
url: 'resources/composeMail/send',
data: {
newEmail : newEmail
},
headers: {'Content-Type':'application/json'}
});
}
return response;
});
这是被调用的类
@Path("/composeMail")
public class Compose {
@POST
@Path("/send")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String sendMessage(NewEmail newEmail) throws MessagingException,
IOException {
System.out.println("I am here");
.....
这里是 NewEmail 对象
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class NewEmail {
private String to;
private String from;
private String subject;
private String body;
public NewEmail(String to, String from, String subject, String body) {
this.to = to;
this.from = from;
this.subject = subject;
this.body = body;
}
......
Java 类没有被调用。我收到 400 响应(错误请求)。如果我从构造函数中删除“NewEmail newEmail”,则该类会被调用并且我会看到预期的输出。
谢谢。
【问题讨论】:
-
我猜你在
NewEmail类中需要一个带有getter 和setter(遵循Java bean 命名约定)的默认(无参数)构造函数。您的反序列化器(默认情况下)可能不知道传递构造函数参数来创建对象 -
您是否尝试过发送未包装的普通对象? IE。而不是:
data: { newEmail : newEmail },,做data: newEmail。 -
这行得通。谢谢。
标签: java angularjs rest jax-rs angularjs-service