使用JQuery和JSON将对象发送到JAX-RS时出错

时间:2012-07-10 09:12:30

标签: java javascript jquery jax-rs

我正在尝试将javascript对象发送到JAX-RS服务器。当我从jquery中获取PUT时,我得到了一个服务器端异常。

这是我的服务器代码

//JAX-RS Server
@PUT
@Path("/edit/{project_key}/")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response editAssociation(@PathParam("project_key") String projectKey,
    @FormParam("association") EditFieldAssociationModel association) {
        //TODO
        //Here i just put a debug break point
        return Response.noContent().build();
}

//EditFieldAssociationModel class    
@XmlRootElement(name = "EditFieldAssociationModel")
@XmlAccessorType(XmlAccessType.FIELD)
public class EditFieldAssociationModel {
    @XmlElement(name = "id")
    private Long id;

    @XmlElement(name = "method")
    private String method;

    @XmlElement(name = "jiraDefaultValue")
    private String jiraDefaultValue;

    @XmlElement(name = "externalDefaultValue")
    private String externalDefaultValue;

    //and public accessors
}

我的js代码:

function(){
    var editasso = {id:asso_id,
            method:d.find(".sync-plugin-method").find('select').val(),
            jiraDefaultValue:d.find('.sync-plugin-jira-default-value').find('select').val(),
            externalDefaultValue:d.find('.sync-plugin-external-default-value').find('select').val()};

    AJS.$.ajax({
          url: AJS.contextPath()+"/rest/a-sync-rest/1.0/association/edit/"+getProjectKey(),
          type: "PUT",
          //TODO

          data: JSON.stringify(editasso),
          beforeSend: function(x) {
                if (x && x.overrideMimeType) {
                  //x.overrideMimeType("application/json; charset=UTF-8");
                  x.setRequestHeader("Content-type", "application/json; charset=UTF-8");
                  console.log("mime type override ok");
                }else{
                    console.log("unable to override mime type");
                }
           },
          dataType: "json",
          success: function(msg){
              dialog.remove();
              location.reload();
          },
          error : sync_plugin_error
    });
}

我尝试使用editasso的其他值

var editasso = {association : {id:asso_id,
                        method:d.find(".sync-plugin-method").find('select').val(),
                        jiraDefaultValue:d.find('.sync-plugin-jira-default-value').find('select').val(),
                        externalDefaultValue:d.find('.sync-plugin-external-default-value').find('select').val()}};

帖子正文似乎是正确的:

{"association":{"id":"350","method":"com.a.jira.synchronization.KeepLatestSyncMethod","jiraDefaultValue":"Valeur 1","externalDefaultValue":"aa"}}

例外是

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

有什么建议吗?

THX

1 个答案:

答案 0 :(得分:0)

问题已解决:EditFieldAssociationModel不得使用与服务器到客户端类相同的注释。现在,我的班级看起来像:

public class EditFieldAssociationModel {
    @JsonProperty("id")
    private Long id;

    @JsonProperty("method")
    private String method;

    @JsonProperty("jiraDefaultValue")
    private String jiraDefaultValue;

    @JsonProperty("externalDefaultValue")
    private String externalDefaultValue;

    //getters and setters
}

它有效