2015-09-27 82 views
0

我有一个带有一些静态内容的Web应用程序。例如,我有一个发布在http://localhost/images/Head.png的图像。Png的MediaType。 HTTP请求

现在,我正在对此映像执行Http请求。

ResponseEntity<byte[]> entity = new TestRestTemplate().getForEntity("http://localhost/images/Head.png", byte[].class);` 

我想知道哪个内容有这个实体。它应该是image/png,但它不是。我在这里得到一个例外:

assertEquals("Wrong content type:\n" + entity.getHeaders().getContentType(), 
       MediaType.valueOf("image/png"), entity.getHeaders().getContentType());` 

我应该是哪种内容类型?

谢谢。

+0

您的代码是否在公开回购?你能提供一个链接吗? –

+0

你可以看看它[这里](https://github.com/Santi-7/hello/blob/master/src/test/java/es/unizar/webeng/hello/SystemTests.java) –

+0

为什么代码你提到的是评论?另外,哪个是JUnit测试的输出?甚至,为什么不在屏幕上打印内容类型的值。如果您发现解决方案,请亲自回答此问题并将其标记为已关闭! –

回答

0

如果运行

$ gradle check 

控制台报告的东西如下

FAILURE: Build failed with an exception 
* What went wrong: 
Execution failed for task ':test'. 
> There were failing tests. See the report at: 
    (...)/hello-master/build/reports/tests/index.html 

BUILD FAILED 

如果你用浏览器打开该文件,你可以看到这一点:

enter image description here

如果您点击系统测试可以看到

enter image description here

这意味着,预期的内容类型是图像/ PNG;字符集= UTF-8。这种内容类型没有错,但怪癖。这是因为默认情况下Spring Boot被迫将配置的字符集添加到内容类型中。您可以在application.properties中将spring.http.encoding.force的属性设置为false,因此返回的内容类型将为image/png。请小心,因为此设置可以修改现有测试的行为。

+0

它的工作!感谢您提供调试建议并解决它。 –