gpt4 book ai didi

使用 mockmvc 的 Spring 集成测试抛出 org.springframework.web.HttpMediaTypeNotSupportedException : Content type 'application/json' not supported

转载 作者:行者123 更新时间:2023-11-28 20:41:01 26 4
gpt4 key购买 nike

我在 spring 集成测试中遇到了一个奇怪的问题。我有一个在 tomcat 上运行的工作 Controller 和应用程序。但是在我的集成测试中,我得到以下堆栈跟踪:

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());
}
}

还有我的 Controller 方法:

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

dto:

public class InvoiceSpecificationDTO {

private List<Long> taskIDs;
private String invoiceDate;
private String dueDate;

public List<Long> getTaskIDs() {
return taskIDs;
}
public void setTaskIDs(List<Long> taskIDs) {
this.taskIDs = taskIDs;
}
public String getInvoiceDate() {
return invoiceDate;
}
public void setInvoiceDate(String invoiceDate) {
this.invoiceDate = invoiceDate;
}
public String getDueDate() {
return dueDate;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
}

我的 gradle.build

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'
}
}

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

mockMvc = standaloneSetup(controller)
.setMessageConverters(new MappingJackson2HttpMessageConverter())
.build();

如果有人对如何前进有任何建议,我们将不胜感激!!

已解决 - 我的根配置中有一个 excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class) 以获取抛出的传递 jsp 运行时异常。当我删除它并在我的类路径中包含一个服务器运行时时,事情开始起作用。

谢谢卡尔

最佳答案

我有一个

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

在我的根配置中获取传递抛出的 jsp 运行时异常。当我删除它并在我的类路径中包含一个服务器运行时时,事情开始起作用。

关于使用 mockmvc 的 Spring 集成测试抛出 org.springframework.web.HttpMediaTypeNotSupportedException : Content type 'application/json' not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32763416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com