如何在Java中的RESTful(Web服务)POST方法中接收json对象 [英] How to receive json object in the RESTful (WEB SERVICE) POST method in Java

查看:929
本文介绍了如何在Java中的RESTful(Web服务)POST方法中接收json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我困惑是否有可能在RESTful网络服务下的POST方法中通过@FormParam接收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;
}

谢谢.

解决方案

这是可能的,但是您需要了解什么是@FormParam,基本上它会从特定的html表单中接收值,就像您可能知道@FormParam一样您想使用@FormParam("myParam")从请求中获取的参数是什么,如果要使用json,则需要提供它.所以答案是您不需要使用@FormParam来使用JSON

上面的方法不是发送一对键/值属性,而是发送完整的json,显然,您可能需要使用jquery甚至javascript生成该json,然后将其发送到您的终结点,就像这样.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/fetch/")
public List<MyObject> getCandidate(MyBean mybean){}

其中MyBean类需要具有要在JSON中发送的字段.这里的神奇之处在于要感谢MessageBodyReader,您应该在类路径中包含杰克逊,您可以找到示例,也是一个好主意. /p>

It may be bit silly question, but I am confuse if it is possible to receive the JSON Objects in the @FormParam in the POST method under RESTful webservice

@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;
}

And what if I want to get the userdefine object in the form param.

@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;
}

Thanks in advance.

解决方案

This is possible however you need to understand what is @FormParam, basically it receive the values from an specific html form, as you probably know @FormParam need to know what is the param you want to take from the request using @FormParam("myParam"), if you want to consume json you need to provide it. So answer is you dont need to use @FormParam to consume JSON

The above means instead of send a pair key/value properties you need to send the full json, obviously you need to generate that json using probably jquery or even javascript then sent that to your endpoint that should be something like.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/fetch/")
public List<MyObject> getCandidate(MyBean mybean){}

Where MyBean class need to have the fields that are going to be sent within the JSON. The magic here is thanks to the MessageBodyReader, you should have jackson in your classpath, you can find an example here. Also will be good idea if you read this.

这篇关于如何在Java中的RESTful(Web服务)POST方法中接收json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆