json - 在单元测试中打印 Jersey JSON

标签 json jaxb jersey

有没有什么简单的方法

  • 打印 JAXB 注释实例
  • JSON 格式(正如 Jersey 将在 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();
    

    对于 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());
    ...
    

    您可以使用 StringWriterString已编码的 JSON 输出的表示。

    关于json - 在单元测试中打印 Jersey JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10593543/

    相关文章:

    java - 如何解码 JAXB 中的 &lt;!DOCTYPE> 部分?

    jersey - OSGi、Jersey 和 "No message body writer has been found for class"

    java - REST:从位于 Web 项目的 "/web-inf/lib"内的 jar 运行 main 方法

    java - 如何在返回对象的 Spring MVC @RestController @ResponseBody 类中响应 HTTP 状态代码?

    python - 将 django 升级到 1.6.5 后出现 django object is not JSON serializable 错误

    json - 如何在 jax-rs 的 post 请求中处理 Json 主体

    java - Java 中用于 REST 服务的异步页面等效项

    ios - 如何将图像数组转换为字符串

    javascript - 在 javascript/jquery 中从 JSON 中删除一个元素

    jaxb - 动态moxy中枚举中的ClassNotFound异常