在单元测试中打印Jersey JSON [英] Print Jersey JSON in Unit Testing

查看:58
本文介绍了在单元测试中打印Jersey JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何简单的方法


  • 以JSON格式打印JAXB带注释的实例

  • (正如运动将在AS中打印)

  • 在运行单元测试时

  • 没有启动任何嵌入式服务器?

  • to print a JAXB annotated instance
  • in JSON format (as exactly jersey will print in AS)
  • while running unit testing
  • without any embedded server launched?

我可以使用JAXBContext,Marshaller等运行并查看XML。

I can run and see in XML with JAXBContext, Marshaller, and so on.

是否有任何示例JSON?

Is there any example for JSON?

我找到了它。它与JAXB类似。

I found it. It's similar to JAXB.

使用 Jersey API

final JSONJAXBContext context = new JSONJAXBContext(...);

final JSONMarshaller marshaller = context.createJSONMarshaller();
marshaller.marshallToJSON(...);

final JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller();
final T unmarshalled = unmarshaller.unmarshalJAXBElementFromJSON(
        ..., T.class).getValue();






for Maven


for Maven

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-json</artifactId>
  <scope>test</scope>
</dependency>


推荐答案

有可能:

...
StringWriter writer = new StringWriter();
marshaller.marshallToJSON(objectToMarshall, writer)
logger.debug(writer.toString());
...

您可以使用 StringWriter 获取编组的JSON输出的 String 表示。

You can use an StringWriter to take the String representation of the marshalled JSON output.

这篇关于在单元测试中打印Jersey JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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