Spring:使用 ResponseEntity<Void> 返回空的 HTTP 响应不起作用

新手上路,请多包涵

我们正在使用 Spring (4.1.1.) 实现 REST API。对于某些 HTTP 请求,我们希望返回一个没有正文的头部作为响应。但是,使用 ResponseEntity<Void> 似乎不起作用。当调用 MockMvc 测试时,返回 406(不可接受)。使用 ResponseEntity<String> 没有参数值( new ResponseEntity<String>( HttpStatus.NOT_FOUND ) )工作正常。

方法:

 @RequestMapping( method = RequestMethod.HEAD, value = Constants.KEY )
public ResponseEntity<Void> taxonomyPackageExists( @PathVariable final String key ) {

    LOG.debug( "taxonomyPackageExists queried with key: {0}", key ); //$NON-NLS-1$

    final TaxonomyKey taxonomyKey = TaxonomyKey.fromString( key );

    LOG.debug( "Taxonomy key created: {0}", taxonomyKey ); //$NON-NLS-1$

    if ( this.xbrlInstanceValidator.taxonomyPackageExists( taxonomyKey ) ) {

        LOG.debug( "Taxonomy package with key: {0} exists.", taxonomyKey ); //$NON-NLS-1$

        return new ResponseEntity<Void>( HttpStatus.OK );

    } else {

        LOG.debug( "Taxonomy package with key: {0} does NOT exist.", taxonomyKey ); //$NON-NLS-1$

        return new ResponseEntity<Void>( HttpStatus.NOT_FOUND );
    }

}

测试用例(TestNG):

 public class TaxonomyQueryControllerTest {

private XbrlInstanceValidator   xbrlInstanceValidatorMock;
private TaxonomyQueryController underTest;
private MockMvc                 mockMvc;

@BeforeMethod
public void setUp() {
    this.xbrlInstanceValidatorMock = createMock( XbrlInstanceValidator.class );
    this.underTest = new TaxonomyQueryController( this.xbrlInstanceValidatorMock );
    this.mockMvc = MockMvcBuilders.standaloneSetup( this.underTest ).build();
}

@Test
public void taxonomyPackageDoesNotExist() throws Exception {
    // record
    expect( this.xbrlInstanceValidatorMock.taxonomyPackageExists( anyObject( TaxonomyKey.class ) ) ).andStubReturn(
            false );

    // replay
    replay( this.xbrlInstanceValidatorMock );

    // do the test
    final String taxonomyKey = RestDataFixture.taxonomyKeyString;

    this.mockMvc.perform( head( "/taxonomypackages/{key}", taxonomyKey ).accept( //$NON-NLS-1$
            MediaType.APPLICATION_XML ) ).andExpect( status().isNotFound() );

}

}

此堆栈跟踪失败:

 FAILED: taxonomyPackageDoesNotExist
java.lang.AssertionError: Status expected:<404> but was:<406>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:652)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:153)
at de.zeb.control.application.xbrlstandalonevalidator.restservice.TaxonomyQueryControllerTest.taxonomyPackageDoesNotExist(TaxonomyQueryControllerTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

原文由 Matthias T 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

注意:这对于问题中提到的版本 4.1.1.RELEASE 是正确的。

Spring MVC 通过 HttpEntityMethodProcessor 处理 ResponseEntity 返回值。

ResponseEntity 值没有设置主体时,就像您的代码片段中的情况一样, HttpEntityMethodProcessor 尝试从参数化中确定响应主体的内容类型 ResponseEntity --- 处理方法签名中的返回类型 @RequestMapping 处理方法。

因此对于

public ResponseEntity<Void> taxonomyPackageExists( @PathVariable final String key ) {

该类型将是 VoidHttpEntityMethodProcessor 然后将遍历其所有注册的 HttpMessageConverter 实例并找到一个可以为 Void 类型编写主体的实例。根据您的配置,它可能找到也可能找不到。

如果它确实找到任何内容,它仍然需要确保相应的正文将使用与请求的 Accept 标头, application/xml 中提供的类型相匹配的 Content-Type 编写。 --- 在你的情况下。

如果在所有这些检查之后,没有这样 HttpMessageConverter 存在,Spring MVC 将决定它不能产生可接受的响应,因此返回 406 Not Acceptable HTTP 响应。

使用 ResponseEntity<String> ,Spring 将使用 String 作为响应主体并查找 StringHttpMessageConverter 作为处理程序。由于 StringHttpMessageHandler 可以为任何媒体类型生成内容(在 Accept 标头中提供),它将能够处理 application/xml 正在请求您的客户

如果 ResponseEntity 中的主体不是 null ,则 Spring MVC 已更改为仅返回 406。如果您使用的是更新版本的 Spring MVC,您将看不到原始问题中的行为。


iddy85 的解决方案中,它似乎暗示 ResponseEntity<?> ,身体的类型将被推断为 Object 。如果您的类路径中有正确的库,即。 Jackson(版本 > 2.5.0)及其 XML 扩展,Spring MVC 将有权访问 MappingJackson2XmlHttpMessageConverter 它可以用来生成 application/xml 类型 Object 他们的解决方案仅在这些条件下有效。 否则,它会因为我上面描述的相同原因而失败。

原文由 Sotirios Delimanolis 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题