您好,我正在编写一个休息服务,用户在对话框中输入值并通过 Ajax 更新 Activity 对象表。但我不断得到 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");
});
});
})
最佳答案
message "Unrecognized field "input" (Class com.leanpitch.leangears.jira.webwork.beans.ActorBeans), not marked as ignorable"
看看这里有什么
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
,我认为它没有被发送。据我所知,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
字符串化。
关于java - 如何解决 REST 服务 : Unrecognized field "input" 引发的未捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796984/