我试图在POST请求中从JSON中提取参数。这似乎是一个非常基本的过程,我已经阅读了许多有关此的文章,但是由于要取回一个对象,但该对象中的字段为空,因此我在这里丢失了一些东西。在我的POST中,我有以下JSON ...
{
"client": "1",
"forTopic": "topic"
}
这是我的servlet中的POST方法...
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类...
@XmlRootElement
public class ClientAndTopicParameters
{
@XmlElement public String client;
@XmlElement public String forTopic;
}
结果输出如下...
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
答案 0 :(得分:1)
解决方案是序列化所需的对象并使用includeRoot标志播放。如果将其设置为false,则将获得所需的输出。
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>
在您的代码中,这是唯一要做的更改:
unmarshal.setProperty (UnmarshallerProperties.JSON_INCLUDE_ROOT, false);