Java RESTful org.codehaus.jettison.json.JSONException:JSONObject文本必须以';{';在字符0处
我正在努力解决一个与使用JSON调用Java RESTful服务相关的问题。RESTful服务只是使用JPA更新数据库表中的记录。奇怪的是,这个问题只在我提交PUT请求时发生。如果我使用POST请求,它可以正常工作。我从服务器端收到以下错误 2014年5月9日下午2:40:32 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper响应 警告:已捕获WebApplicationException: org.codehaus.jettison.json.JSONException:JSONObject文本必须以 在的字符0处使用“{” 发送的JSON没有问题。发送的JSON如下所示Java RESTful org.codehaus.jettison.json.JSONException:JSONObject文本必须以';{';在字符0处,java,json,rest,jpa,Java,Json,Rest,Jpa,我正在努力解决一个与使用JSON调用Java RESTful服务相关的问题。RESTful服务只是使用JPA更新数据库表中的记录。奇怪的是,这个问题只在我提交PUT请求时发生。如果我使用POST请求,它可以正常工作。我从服务器端收到以下错误 2014年5月9日下午2:40:32 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper响应 警告:已捕获WebApplicationException: org.codehaus.jettison
{
"workflowMilestone": {
"id": 7,
"wfId": {
"id": "1"
},
"orderNum": "2",
"description": "Week 2",
"completion": "1",
"completionLengthCode": {
"lengthCode": "WEEKS"
}
}
}
为了从客户端调用RESTful服务,我正在创建一个HttpURLConnection,如下所示
URL url = new URL(urlStr); // urlStr contains the URL being called
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.setRequestProperty("Accept", "application/json");
httpCon.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter output = null;
try {
output = new OutputStreamWriter(httpCon.getOutputStream());
output.write(jsonString.trim());
int responseCode = httpCon.getResponseCode();
System.out.println("PUT Response Code: " + responseCode);
} finally {
if (output != null) { output.close(); }
}
从服务器端看,RESTful方法如下所示
@Path("/rest/wfService")
@Produces(MediaType.APPLICATION_JSON)
@Stateless(name = "caWorkflowService")
public class WorkflowService extends BaseService {
...
@PUT
@Path("/updateWfMilestone")
@Consumes(MediaType.APPLICATION_JSON)
public WorkflowMilestone updateWfMilestone(
@Context HttpHeaders headers,
@Context HttpServletRequest request,
final WorkflowMilestone workflowMilestone) {
...
try {
...
WorkflowMilestone toUpdate = this.getEntityManager().merge(workflowMilestone);
return toUpdate;
} catch (IllegalArgumentException iae) {
LOGGER.log(Level.SEVERE, "Unable to update WorkflowMilestone record.", iae);
workflowMilestone.setError("IAE: Unable to update WorkflowMilestone record.");
return workflowMilestone;
} catch (EntityExistsException nre) {
LOGGER.log(Level.SEVERE, "Unable to create WorkflowMilestone record.", nre);
workflowMilestone.setError("EEE: Unable to update WorkflowMilestone record.");
return workflowMilestone;
} catch (TransactionRequiredException tre) {
LOGGER.log(Level.SEVERE, "Unable to update WorkflowMilestone record.", tre);
workflowMilestone.setError("TRE: Unable to update WorkflowMilestone record.");
return workflowMilestone;
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Unable to update WorkflowMilestone record.", t);
workflowMilestone.setError("T: Unable to update WorkflowMilestone record.");
return workflowMilestone;
}
}
...
}
您可以在此处看到正在使用的模型:
既然POST请求可以很好地工作,我可以使用它,但我宁愿坚持标准并使用PUT
我已经研究了几个小时的解决方案,但没有成功。如果您能告诉我我可能在这里做错了什么,我将不胜感激。谢谢。那么,您遇到了这个错误。它是在服务器端还是客户端?它来自服务器端。我会尝试的第一件事是使用已知的REST测试工具,如SoapUI或Google Chrome的REST控制台。看看你是否能够使用这些工具复制它。JSON在我看来很好。你的模型对象上有任何自定义注释吗?你可以发布你的模型吗?模型中没有任何真正的特殊注释。你可以在这里看到: