2
这可能有点愚蠢的问题,但如果可以在RESTful web服务下的POST方法中的@FormParam中接收JSON对象,我很困惑如何在Java中的RESTful(WEB SERVICE)POST方法中接收json对象
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/fetch/")
public List<MyObject> getCandidate(@FormParam("CodeList")
List<String> codeList)
{
List<MyObject> myobjects = null;
try {
//some code
} catch (Exception e) {
//some exception if occur
}
return myobjects;
}
而如果我想获得param形式的userdefine对象。
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/fetch/")
public List<MyObject> getCandidate(@FormParam("CodeList")
List<MyObject> formMyObjectList)
{
List<MyObject> myobjects = null;
try {
//some code
} catch (Exception e) {
//some exception if occur
}
return myobjects;
}
在此先感谢。
恩,你试过了吗?提示:无论如何,它都使用'@ POST'工作。 – Smutje 2015-02-11 07:33:10
您需要将其作为字符串获取并自行解组,或者编写MessageBodyReader。前者可能更容易,见[这里](http://stackoverflow.com/a/27949340/2587435)。尝试使用另一种类型的方法参数,您将需要一个自定义提供程序。否则,你为什么要接受作为一个表单参数呢?使用表单参数,你应该@Consume(“application/x-www-form-urlencod”)'。如果你想'应用程序/ json'然后发送身体为JSON – 2015-02-11 07:36:01
感谢您的回应Smutje。如何通过将整个对象发送到REST来检查相同的内容。 我有很多数据要接收,所以我不想为对象的每个单独元素使用@FormParam。我想收到它作为一个整体对象,所以我可以执行它的登录。 – 2015-02-11 07:42:57