自定义活页夹在PlayFramework中不起作用

自定义活页夹在PlayFramework中不起作用,playframework,Playframework,我在控制器操作中显式使用带有@As关键字的TypeBinder。无论我做什么,bind方法都不会被调用 什么决定了所使用的粘合剂 我还将日志设置为ALL,我意识到Binder.bindInternal也从未被调用过 这是我的活页夹 public class EmailTypeBinder implements TypeBinder<Email> { @Override public Email bind(String name, Annotation[] anno

我在控制器操作中显式使用带有@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);     
    }

}
下面是调用控制器的响应

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>();
类电子邮件实现可序列化{
//工厂方法
公共静态电子邮件createSimple(字符串主题、字符串消息){
返回新电子邮件(“”,主题,消息);
}
公共静态电子邮件创建(字符串来源、字符串主题、字符串消息){
返回新电子邮件(发件人、主题、邮件);
}
公共静态电子邮件createWithTemplate(字符串来源、字符串主题、字符串消息、字符串模板){
电子邮件=新电子邮件(发件人、主题、消息);
email.setTemplate(模板);
回复邮件;
}   
公共字符串getFrom(){
返乡;
}
公共字符串getSubject(){
返回主题;
}
公共字符串getMessage(){
返回消息;
}
公共集合getGroupList(){
返回群组列表;
}
公共集getGroups(){
返回组;
}
public void addGroupList(字符串groupList){
groupList.add(groupList);
}
公共void addGroup(字符串组){
组。添加(组);
}
私有void setTemplate(字符串模板){
this.template=模板;
}
//我们必须用工厂的方法来创造我们
私人电子邮件(字符串发件人、字符串主题、字符串消息){
this.from=from;
this.subject=主语;
this.message=消息;
}
//仅用于Gson JSON序列化/反序列化
公共电子邮件(){}
来自的私有字符串;
私有字符串主题;
私有字符串消息;
私有字符串模板;
私有集组=新HashSet();
private Set grouplist=new HashSet();
}

我唯一能让它工作的方法是使用Play Framework Cookbook中第4章示例中的ApiPlugin
我可以使用它,但是框架说它应该能够通过定义一个TypeBinder来处理它,但事实并非如此

您似乎将电子邮件对象作为json对象提供,但绑定器在表单参数上工作。尝试将对象作为form param提供,或使用gson对对象进行反序列化

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>();