JAXB Unmarshal JSON HTTP POST参数 [英] JAXB Unmarshal JSON HTTP POST Parameters

查看:118
本文介绍了JAXB Unmarshal JSON HTTP POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在POST请求中从JSON中提取参数。这似乎是一个非常基本的过程,我已经阅读了很多关于此的帖子,但我在这里遗漏了一些东西,因为我得到一个对象,但该对象中的字段为空。在我的POST中,我有以下JSON ...

I'm attempted to pull parameters from JSON in a POST request. This seems like a very basic process and I've read through numerous posts about this but I'm missing something here as I'm getting an object back but the fields within that object are null. In my POST I have the following JSON...

{
  "client": "1",
  "forTopic": "topic"
}

这里是我的POST方法我的servlet ...

And here is my POST method inside my servlet...

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{           
    String requestBody = RESTUtil.getRequestBody (request);
    log.debug (requestBody);

    try
    {
        JAXBContext context = JAXBContext.newInstance (ClientAndTopicParameters.class);

        Unmarshaller unmarshal = context.createUnmarshaller ();

        unmarshal.setProperty (UnmarshallerProperties.MEDIA_TYPE, "application/json");
        unmarshal.setProperty (UnmarshallerProperties.JSON_INCLUDE_ROOT, true);

        ClientAndTopicParameters params = (ClientAndTopicParameters) unmarshal.unmarshal (new StreamSource (new StringReader (requestBody)), ClientAndTopicParameters.class).getValue ();

        log.debug ("params = " + params);
        log.debug ("client = " + params.client);
        log.debug ("forTopic = " + params.forTopic);
    }
    catch (JAXBException e)
    {
        log.error ("Unable to get Client and Topic parameters from POST.", e);
    }
}

最后,这是我的ClientAndTopicParameters类...

Finally, here is my ClientAndTopicParameters class...

@XmlRootElement
public class ClientAndTopicParameters
{
    @XmlElement public String                       client;
    @XmlElement public String                       forTopic;
}

结果输出如下......

The resulting output is the following...

2018 Aug 24 17:44:55,806 DEBUG [MyServlet                        ] params = mypackage.ClientAndTopicParameters@2995a298
2018 Aug 24 17:44:55,806 DEBUG [MyServlet                        ] client = null
2018 Aug 24 17:44:55,806 DEBUG [MyServlet                        ] forTopic = null

你可以看到这个非常基本的东西。我假设我错过了一些我没看到的小东西。欢迎任何想法和见解。作为参考,我正在使用JAXB v2.3.0

As you can see this pretty basic stuff. I'm assuming I'm missing something small that I'm just not seeing. Welcome any thoughts and insights. For reference I'm using JAXB v2.3.0

推荐答案

解决方案是序列化您想要的对象并使用includeRoot标志进行播放。如果将其设置为false,您将获得所需的输出。

The solution is to serialize your desired object and play with the includeRoot flag. If you set it to false, you will get the desired output.

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.UnmarshallerProperties;

@XmlRootElement
public class ClientAndTopicParameters
{

    @XmlElement public String                       client;
    @XmlElement public String                       forTopic;


    public static void main(String[] args) {

        try
        {
            boolean includeRoot = true;

            JAXBContext context = JAXBContext.newInstance     (ClientAndTopicParameters.class);
            Unmarshaller unmarshal = context.createUnmarshaller ();
            unmarshal.setProperty (UnmarshallerProperties.MEDIA_TYPE,     "application/json");
            unmarshal.setProperty (UnmarshallerProperties.JSON_INCLUDE_ROOT,     includeRoot);

            parseAndPrint(unmarshal, "{ \"client\": \"1\",  \"forTopic\": \"topic\"}");
            StringWriter sw = marshallDesiredObject(context, includeRoot);
            parseAndPrint(unmarshal, sw.toString());
        }
        catch (JAXBException e)
        {
            System.out.println("Unable to get Client and Topic parameters from POST.");
            e.printStackTrace();
        }
    }

    private static StringWriter marshallDesiredObject(JAXBContext context, boolean includeRoot)
        throws JAXBException, PropertyException {
        Marshaller marshal = context.createMarshaller ();

        marshal.setProperty (UnmarshallerProperties.MEDIA_TYPE, "application/json");
        marshal.setProperty (UnmarshallerProperties.JSON_INCLUDE_ROOT, includeRoot);

        ClientAndTopicParameters cp = new ClientAndTopicParameters();
        cp.client = "1";
        cp.forTopic = "topic";

        StringWriter sw = new StringWriter();
        marshal.marshal(cp, sw);
        return sw;
    }

    private static void parseAndPrint(Unmarshaller unmarshal, String requestBody)
            throws JAXBException {
        System.out.println("requestBody to parse: " + requestBody);
        ClientAndTopicParameters params = unmarshal.unmarshal(new StreamSource (new     StringReader (requestBody )), ClientAndTopicParameters.class).getValue ();
        System.out.println("params = " + params);
        System.out.println("client = " + params.client);
        System.out.println("forTopic = " + params.forTopic);
    }
}

我使用了这些依赖项:

        <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.moxy</artifactId>
        <version>2.5.2</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.11</version>
    </dependency>

在您的代码中,这是您唯一需要更改的内容:

In your code, this is the only thing you have to change:

unmarshal.setProperty (UnmarshallerProperties.JSON_INCLUDE_ROOT, false);

这篇关于JAXB Unmarshal JSON HTTP POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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