Spring与mockmvc的集成测试抛出org.springframework.web.HttpMediaTypeNotSupportedException:内容类型';application/json';不支持

Spring与mockmvc的集成测试抛出org.springframework.web.HttpMediaTypeNotSupportedException:内容类型';application/json';不支持,spring,spring-mvc,testing,integration-testing,mockmvc,Spring,Spring Mvc,Testing,Integration Testing,Mockmvc,我对spring集成测试有一个奇怪的问题。我有一个运行在tomcat上的工作控制器和应用程序。然而,在我的集成测试中,我得到了以下跟踪: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.rea

我对spring集成测试有一个奇怪的问题。我有一个运行在tomcat上的工作控制器和应用程序。然而,在我的集成测试中,我得到了以下跟踪:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:651)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:610)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:173)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:442)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:430)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:156)
at com.test......
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我的测试如下所示:

@Category(DbIntegrationTest.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={RootConfig.class})
@WebAppConfiguration
@ActiveProfiles({"dev", "fk_mock"})
public class WhenUserSubmitsNew  {

@Autowired private WebApplicationContext context;
protected MockMvc mvc;

@Before
public void setup() {
    buildMockMVC();
    createBasicUserSetup();
}

public void buildMockMVC() {

    mvc = MockMvcBuilders
            .webAppContextSetup(context)
            //.apply(springSecurity())
            .build();
}

public void createBasicUserSetup() {
    ...
}


@Test
public void shouldCreate() throws Exception {
    String date1 = "2015-05-05";
    String date2 = "2015-07-30";

    String json = "{\"taskIDs\": [5, 6], \"invoiceDate\":\""+date1+"\", \"dueDate\":\""+date2+"\"}";

    String url = "/submitNew";
    mvc.perform(post(url)
            .with(user(user.getUserName()))
            .sessionAttr(AbstractController.USER_SESSION_ATTR, user)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .content(json))
        .andExpect(status().isCreated());
}
} 
和我的控制器方法:

    @RequestMapping(value="/submitNew", method=RequestMethod.POST)
public @ResponseBody Long create(HttpSession session, @RequestBody InvoiceSpecificationDTO form) {
    ...
}
dto:

为了让事情变得更有趣,我还对使用独立设置的控制器进行了单元测试:

mockMvc = standaloneSetup(controller)
            .setMessageConverters(new MappingJackson2HttpMessageConverter())
            .build();
如果有人对如何前进有任何建议,我们将不胜感激

已解决-我在根配置中有一个excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class),以获取引发的pass jsp运行时异常。当我删除该异常并在类路径上包含一个serverruntime时,一切都开始工作

谢谢 卡尔

我有一个

excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)


在我的根配置中获取传递抛出的jsp运行时异常。当我删除该异常并在类路径中包含serverruntime时,一切开始工作。

出于好奇,您为什么在gradle构建文件中对该版本进行了注释?如果我没有错,请将您的DTO粘贴到CustomForm.java>中,这是一个问题盖特和塞特file@AlbertPinto为什么你在你的gradle构建文件中评论了这个版本?当我更新我的gradle构建时,我注意到我不需要它。你也可以将你的DTO粘贴到CustomForm.java>如果我没有错,该文件中的getter和setter会有问题,但是因为我有一个更统一的t使用较小的设置进行测试风格测试,我认为这不是问题所在。@AlbertPinto我已经删除了我的应用程序特定代码,这就是json字符串看起来有点滑稽的原因。
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
group = '...'
version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
   springVersion = '4.2.0.RELEASE'
   springSecurityVersion = '4.0.2.RELEASE'
   springWSVersion = '2.2.2.RELEASE'
   aspectjVersion = '1.6.9'
   slf4jVersion = '1.7.5'
   tilesVersion = '3.0.5'
   jsonPathVersion = '0.8.1'
   servletApiVersion = '3.0.1'
   jacksonDatabindVersion = '2.6.1'
   h2Version = '1.3.156'
   postgresqlVersion = '9.3-1100-jdbc4'
   hibEntitymanagerVersion = '4.3.8.Final'
   //hibValidatorVersion = '4.1.0.Final'
   javaxInjectVersion = '1'
   dbcpVersion = '1.4'
   hamcrestVersion = '1.3'
   junitVersion = '4.11'
   mockitoCoreVersion = '1.9.5'
   jstlVersion = '1.2'
   slf4jAPI     = "org.slf4j:slf4j-api:${slf4jVersion}"
   slf4jJCL     = "org.slf4j:jcl-over-slf4j:${slf4jVersion}"
   slf4jLog4J   = "org.slf4j:slf4j-log4j12:${slf4jVersion}"
   log4JVersion = '1.2.16'
   log4J        = "log4j:log4j:${log4JVersion}"
}
repositories {
   mavenCentral()
}
dependencies {
    providedCompile "javax.servlet:javax.servlet-api:${servletApiVersion}"  
    //finns i båda build
     compile ("org.springframework:spring-webmvc:${springVersion}") {
        exclude group: 'commons-logging', module: 'commons-logging'
   }
   compile "org.springframework:spring-webmvc:${springVersion}"
   compile "org.springframework:spring-orm:${springVersion}"
   compile "org.springframework.security:spring-security-web:${springSecurityVersion}"
   compile "org.springframework.security:spring-security-config:${springSecurityVersion}"
   compile "org.springframework.ws:spring-ws-core:${springWSVersion}"
   compile "org.springframework.security:spring-security-crypto:${springSecurityVersion}"
//   compile 'org.springframework.data:spring-data-jpa:1.9.0.RELEASE'
   compile "com.fasterxml.jackson.core:jackson-databind:${jacksonDatabindVersion}"
   //compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.1"
   //compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.6.1"
   compile "org.apache.tiles:tiles-extras:${tilesVersion}"
   compile "org.apache.tiles:tiles-jsp:${tilesVersion}"
   compile "com.h2database:h2:${h2Version}"
   compile "org.postgresql:postgresql:${postgresqlVersion}"
   compile "org.hibernate:hibernate-entitymanager:${hibEntitymanagerVersion}"
//   compile "org.hibernate:hibernate-validator:${hibValidatorVersion}"
   compile "javax.inject:javax.inject:${javaxInjectVersion}"
   compile "commons-dbcp:commons-dbcp:${dbcpVersion}"
   testCompile "org.hamcrest:hamcrest-all:${hamcrestVersion}"
   testCompile "junit:junit:${junitVersion}"
   testCompile "org.springframework:spring-test:${springVersion}"
   testCompile "org.mockito:mockito-core:${mockitoCoreVersion}"
   testCompile "com.jayway.jsonpath:json-path:${jsonPathVersion}"
   testCompile "com.jayway.jsonpath:json-path-assert:${jsonPathVersion}"
   testCompile "org.springframework.security:spring-security-test:${springSecurityVersion}"
   runtime "javax.servlet:jstl:${jstlVersion}"
      // Logging:
   compile(slf4jAPI)
   runtime(
       slf4jJCL, slf4jLog4J
   )
   runtime(log4J) {
        exclude group: 'javax.mail',   module: 'mail'
        exclude group: 'javax.jms',    module: 'jms'
        exclude group: 'com.sun.jdmk', module: 'jmxtools'
        exclude group: 'com.sun.jmx',  module: 'jmxri'
   }
}
mockMvc = standaloneSetup(controller)
            .setMessageConverters(new MappingJackson2HttpMessageConverter())
            .build();