RESTful org.codehaus.jettison.json.JSONException:JSONObject文本必须以' {'在角色0

时间:2014-05-09 21:17:26

标签: java json rest jpa

我正在努力解决与使用JSON调用Java RESTful服务相关的问题。 RESTful服务只使用JPA更新数据库表中的记录。奇怪的是,这个问题只发生在我提交PUT请求时。如果我使用POST请求,它可以正常工作。我从服务器端收到以下错误...

  

2014年5月9日下午2:40:32   org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse   警告:WebApplicationException已被捕获:   org.codehaus.jettison.json.JSONException:必须开始JSONObject文本   用' {'在

的0字符处

发送的JSON没有任何问题。正在发送的JSON如下......

{
    "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;
        }
    }

    ...
}

您可以在此处查看正在使用的模型:http://pastebin.com/qACfrNnV

由于POST请求正常,我可以使用它,但我宁愿坚持标准并使用PUT。

我已经研究了几个小时的解决方案而没有成功。我很感激我在这里可能做错的任何想法。谢谢。

0 个答案:

没有答案