1

我想将 HTTP POST 请求正文(以 JSON 格式)转换为 Java 对象。

我在服务器端使用这个 Java 代码:

ObjectMapper mapper = new ObjectMapper();
clip = mapper.readValue(request.body(), Clip.class);

而 JavaScript (jQuery) 客户端代码是:

$.post("/insertClip", { "item": item.value, "value":value.value, "dimension":dimension.value });

当我运行应用程序时,出现此错误:

org.codehaus.jackson.JsonParseException: Unexpected character ('i' (code 105)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: clips.json; line: 1, column: 2]
    at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
    at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:2090)
    at org.codehaus.jackson.impl.Utf8StreamParser._nextTokenNotInObject(Utf8StreamParser.java:606)
    at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:492)
    at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1817)
    at elements.DefinitedRoute$2.handle(DefinitedRoute.java:63)
    at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:133)
    at spark.webserver.JettyHandler.doHandle(JettyHandler.java:47)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:188)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:342)
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:596)
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1068)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:807)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:220)
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:426)
    at org.eclipse.jetty.server.bio.SocketConnector$ConnectorEndPoint.run(SocketConnector.java:241)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:528)
    at java.lang.Thread.run(Thread.java:724)

为什么我无法将此 POST 请求转换为 Java 对象?

4

1 回答 1

2

I have solved the problem adding the JSON.stringify() method in the jQuery code:

 $.post("http://localHost:4567/insertClip", 
JSON.stringify({ "item": item.value , "value":value.value, "dimension":dimension.value }));

Thanks.

于 2013-07-22T07:22:58.167 回答