我在控制器操作中明确使用带有@As关键字的TypeBinder。无论我做什么,都不会调用bind方法。
什么决定了使用的活页夹?
我还将我的日志记录转为ALL,我意识到Binder.bindInternal也从未被调用过。
这是我的活页夹
public class EmailTypeBinder implements TypeBinder<Email> {
@Override
public Email bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception {
System.out.println("Inside email type binder");
return new Gson().fromJson(value, Email.class);
}
}
这是控制器动作
public static void send(@As(binder=EmailTypeBinder.class) Email email) {
try {
for(String key1:DataParser.parsers.keySet()) {
System.out.println("Key: " + key1 + " value: " + DataParser.parsers.get(key1).getClass().getName());
}
for(String key : params.all().keySet()) {
System.out.println("Key: " + key + " value: " + params.get(key));
}
System.out.println("ContentType: " + request.contentType);
if(request.body == null) {
System.out.println("request.body is null");
}
System.out.println("Available bytes: " + request.body.available());
InputStreamReader reader = new InputStreamReader(request.body);
String str;
BufferedReader br = new BufferedReader(reader);
while ((str = br.readLine()) != null) {
System.out.println("You entered String : " + str);
}
if(email == null) {
System.out.println("email is null");
} else {
System.out.println(email.getMessage());
}
}catch(Exception e) {
e.printStackTrace();
}
以下是调用控制器的响应
Key: application/xml value: play.data.parsing.TextParser
Key: multipart/form-data value: play.data.parsing.ApacheMultipartParser
Key: application/json value: play.data.parsing.TextParser
Key: application/x-www-form-urlencoded value: play.data.parsing.UrlEncodedParser
Key: body value: {email:{"from":"","subject":"Test Posted Subject","message":"This is my POSTED message","groups":[],"groupLists":["COS"]}}
ContentType: application/json
Available bytes: 0
email is null
在我的路线中我有
POST /send Application.send
客户端实际上是Groovy,但这并不重要,因为我也试过将它变成一个模型,只是这个项目中的常规对象。无论如何你可以想到
public static void post(Email email) {
def http = new HTTPBuilder( 'http://localhost:9000' )
// perform a GET request, expecting JSON response data
http.request( POST, ContentType.JSON ) { req ->
uri.path = '/send'
body = "{email:" + new Gson().toJson(email) + "}";
// response handler for a success response code:
response.success = { resp, json ->
println "status: ${resp.statusLine}"
// parse the JSON response object:
println " ${json}"
}
// handler for any failure status code:
response.failure = { resp ->
println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
}
}
}
这是我的电子邮件课程,但我不确定其相关的
class Email implements Serializable {
// factory methods
public static Email createSimple(String subject, String message) {
return new Email("", subject, message);
}
public static Email create(String from, String subject, String message) {
return new Email(from, subject, message);
}
public static Email createWithTemplate(String from, String subject, String message, String template) {
Email email = new Email(from, subject, message);
email.setTemplate(template);
return email;
}
public String getFrom() {
return from;
}
public String getSubject() {
return subject;
}
public String getMessage() {
return message;
}
public Set<String> getGroupLists() {
return groupLists;
}
public Set<String> getGroups() {
return groups;
}
public void addGroupList(String groupList) {
groupLists.add(groupList);
}
public void addGroup(String group) {
groups.add(group);
}
private void setTemplate(String template) {
this.template = template;
}
// we must use the factory methods to create us
private Email(String from, String subject, String message){
this.from = from;
this.subject = subject;
this.message = message;
}
// only used for Gson JSON serialization/deserialization
public Email(){}
private String from;
private String subject;
private String message;
private String template;
private Set<String> groups = new HashSet<String>();
private Set<String> groupLists = new HashSet<String>();
}
我能够实现这一目标的唯一方法是使用Play Framework Cookbook中第4章示例中的ApiPlugin 我可以使用它,但框架说它应该能够通过定义TypeBinder来处理它,但事实并非如此。
答案 0 :(得分:0)
您似乎将您的电子邮件对象作为json对象提供,但是绑定器在表单参数上工作。尝试将您的对象作为表单参数提供,或者在游戏中使用gson来反序列化对象