有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从Junit中的JaxRs响应类获取头值、状态代码等

我正在使用Dropwizard使用RESTfulWeb服务。并生成如下响应:

    Response response = resources.client().resource("/url")
    .header("CONTENT-TYPE","value")
    .post(Response.class, jsonRequestString);

现在我想编写单元测试,以确保在响应对象中更正返回的内容类型。怎么做


共 (1) 个答案

  1. # 1 楼答案

    您可以在Jackson中使用ClientResponse类型。例如,使用GET操作:

    ClientResponse response = Client.create()
                                    .resource(url)
                                    .get(ClientResponse.class);
    String contentType = response.getHeaders()
                                 .getFirst("Content-Type");
    System.out.println(contentType);