如何解决REST服务引发的未捕获异常:无法识别的字段“input”

时间:2015-06-12 06:53:31

标签: java ajax json rest jax-rs

您好我正在编写一个休息服务,用户在对话框中输入值,并通过Ajax更新Active对象表。但我一直在努力 REST服务抛出的未捕获异常:无法识别的字段“输入”。

@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Path("/message")
public class LeangearsRestResource {


    ActorConfService ActorConfService;

    private final ApplicationProperties applicationProperties;
    public LeangearsRestResource(ApplicationProperties applicationProperties, ActorConfService actorConfService){
        this.applicationProperties = applicationProperties;
        this.actorConfService = actorConfService;
    }
    static final javax.ws.rs.core.CacheControl NO_CACHE = new javax.ws.rs.core.CacheControl();

    @POST
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response addMessage(ActorBeans actorBeans)
    {

        if(actorConfService.add(actorBeans.getActor(),actorBeans.getRole()))
        {
            return Response.ok(new Result(true)).cacheControl(NO_CACHE).build();
        }
        return Response.ok(new Result(false)).cacheControl(NO_CACHE).build();
    }

ActorBean.java

@XmlAccessorType(XmlAccessType.FIELD)
public class ActorBeans {


/*
    @XmlElement(name = "projectName")
    String productName;*/
    @XmlElement(name = "actor")
    String actor;
    @XmlElement(name = "role")
    String role;

    public ActorBeans() {
    }

    public ActorBeans(String productName, String actor, String role){
        /*this.productName = productName;*/
        this.actor = actor;
        this.role =role;
    }

    /*public void setProductName(String productName) {

        this.productName = productName;
    }

    public String getProductName(){

        return productName;
    }*/


    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

actor.js

function actor_createPostAjaxOptions (data, data1) {
    return {
        "cache": false,
        "contentType": 'application/json',
        "dataType": 'json',
        "data": JSON.stringify(data, data1),
        "processData": false,
        "type": 'POST'
    };
}

function actor_createDeleteAjaxOptions (data) {
    return {
        "cache": false,
        "contentType": 'application/json',
        "dataType": 'json',
        "data": JSON.stringify(data),
        "processData": false,
        "type": 'DELETE'
    };
}




AJS.$( function(){
    // Standard sizes are 400, 600, 800 and 960 pixels wide
    var actor_dialog = new AJS.Dialog({
        width: 400,
        height: 300,
        id: "example-dialog",
        closeOnOutsideClick: true
    });

    // PAGE 0 (first page)
    // adds header for first page
    actor_dialog.addHeader("Actor");

    // add panel 1
    actor_dialog.addPanel("Panel 1", "<input id='dialoginput' type='text' value=''>Actor1</input>" + "<br>" + "<input id='dialoginput1' type='text' value=''>Actor2</input>" , "panel-body");


    actor_dialog.addLink("Cancel", function (actor_dialog) {
        actor_dialog.hide();
    }, "#");



    actor_dialog.addSubmit(
        "Submit",
        function(actor_dialog) {
            actor_dialog.hide();
            AJS.log(AJS.$("#dialoginput").val());
            data = {input:AJS.$("#dialoginput").val()};

            data1 = {input:AJS.$("#dialoginput1").val()};
            jQuery.ajax(
                AJS.params.baseURL+"/rest/leangearsrestresource/1.0/message",
                actor_createPostAjaxOptions(data, data1)
            )
            AJS.$("#test").html(AJS.$("#dialoginput").val())
            AJS.$("#test1").html(AJS.$("#dialoginput1").val())
        }
    );

    // Add events to dialog trigger elements
    AJS.$("#dialog-button").click(function() {
        // PREPARE FOR DISPLAY
        // start first page, first panel
        //debugger;

        //call ajax to get existing value
        jQuery.ajax(
            AJS.params.baseURL+"/rest/leangearsrestresource/1.0/message",
            {
                "cache": false,
                "processData": false,
                "type": 'GET',
                "contentType": 'application/json',
                "dataType": 'json'
            }).done(function(result) {

                AJS.log(result);

                AJS.$("#dialoginput").val(result.value);
                actor_dialog.gotoPage(0);
                actor_dialog.gotoPanel(0);
                actor_dialog.show();



            }).fail(function() {
                AJS.log("failed get GET");

            });


    });

})

1 个答案:

答案 0 :(得分:1)

  

消息“无法识别的字段”输入“(类com.leanpitch.leangears.jira.webwork.beans.ActorBeans),未标记为可忽略的”

看看你在这里有什么

data = {input:AJS.$("#dialoginput").val()};
data1 = {input:AJS.$("#dialoginput1").val()};

actor_createPostAjaxOptions(data, data1)
[..] 
"data": JSON.stringify(data, data1),

信息很清楚。它说你有一个带有“输入”字段的JSON,服务器不知道如何处理。看看这个

data = {input:AJS.$("#dialoginput").val()};

无论AJS.$("#dialoginput").val()是什么,都是"input"字段的值。因此,如果值为"value",则发送的JSON为

{ "input" : "value" }

data1而言,我认为不会发送。 AFAIK,JSON.stringify只应该将一个Javascript对象作为数据参数,即它将进行字符串化的对象。

现在看看你的Java对象

public class ActorBeans {

    @XmlElement(name = "actor")
    String actor;
    @XmlElement(name = "role")
    String role;

这意味着预期的JSON格式为

{ "actor" : "value", "role" : "value" }

JSON与JSON的语法非常接近,因此Javacript对象的外观非常接近。所以你可能有像

这样的Javascript对象
var data = {
    actor: AJS.$("#dialoginput").val(),
    role: AJS.$("#dialoginput1").val()
};

然后,您可以将data字符串化为发送。