我有一个jersey服务器示例,该示例在XML上工作正常,但在JSON下不工作。
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
@Path("JsonExample")
public class JsonExample {
@XmlRootElement
public static class Input {
public String text;
}
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String test(Input i) {
return i.text;
}
}
如果我将此xml请求发送application/xml
到服务器,则一切正常,响应为a
(to http://localhost:8080/App/rest/JsonExample
as POST
)
<input><text>a</text></input>
我把它翻译成JSON在utilities-online.info,把它作为application/json
相同的URL,以相同的参数,但得到一个错误“400错误的请求”
{
"input": { "text": "a" }
}
环境:
我尝试过的
org.codehaus.jackson.jaxrs
到jersey.config.server.provider.packages
初始化参数com.sun.jersey.api.json.POJOMappingFeature
为true
com.sun.jersey
/ jersey-json
/1.8
在写我的问题时,我看到了一个例外,“输入”不应作为字段。正确的JSON请求必须为:
{ "text": "a" }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句