在单元测试中打印Jersey JSON

Jin*_*won 6 json jaxb jersey

有没有简单的方法

  • 打印JAXB带注释的实例
  • 采用JSON格式(正如球衣将在AS中打印)
  • 在运行单元测试时
  • 没有任何嵌入式服务器推出

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

JSON有什么例子吗?


我找到了.它类似于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();
Run Code Online (Sandbox Code Playgroud)

对于Maven

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-json</artifactId>
  <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

log*_*off 2

有可能的:

...
StringWriter writer = new StringWriter();
marshaller.marshallToJSON(objectToMarshall, writer)
logger.debug(writer.toString());
...
Run Code Online (Sandbox Code Playgroud)

您可以使用 anStringWriter来表示String编组的 JSON 输出。