Embedded Jetty返回application/json时提示Missing MessageBodyWriter问题
问题说明
我已在Stack Overflow上查阅了20多个同类问题,但现有解决方案均无法解决我的问题。
运行单元测试时抛出如下报错:
Could not find MessageBodyWriter for response object of type: jetty.test.JettyTest$Health of media type: application/json
我知晓需要引入对应JSON映射Provider,但目前尝试的相关依赖均无法生效,不清楚是否需要排除部分冲突依赖,恳请各位提供解决思路,十分感谢。
测试类代码
package jetty.test; import static io.restassured.RestAssured.given; import io.restassured.builder.RequestSpecBuilder; import io.restassured.filter.log.RequestLoggingFilter; import io.restassured.filter.log.ResponseLoggingFilter; import io.restassured.http.ContentType; import io.restassured.specification.RequestSpecification; import java.util.HashSet; import java.util.Set; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import org.eclipse.jetty.server.Server; import org.junit.jupiter.api.Test; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher; public class JettyTest { @XmlRootElement public static class Health { public Health () {} @XmlElement(name = "status") private String status; public String getStatus() { return status; } public Health setStatus(String status) { this.status = status; return this; } } @Path("/health") public static class HealthResource { public HealthResource() {} @GET @Path("/get") @Produces(MediaType.APPLICATION_JSON) public Response getHealth() { return Response.ok().entity(new Health()).build(); } } public static class TestApplication extends Application { public TestApplication () {} @Override public Set<Class<?>> getClasses() { HashSet<Class<?>> set = new HashSet<>(); set.add(HealthResource.class); return set; } } @Test public void hello() throws Exception { Server server = new Server(8082); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); server.setHandler(context); final ServletHolder servletHolder = new ServletHolder(new HttpServletDispatcher()); servletHolder.setInitParameter("resteasy.servlet.mapping.prefix", "/"); servletHolder.setInitParameter("javax.ws.rs.Application", TestApplication.class.getName()); context.addServlet(servletHolder, "/"); server.start(); RequestSpecification spec = new RequestSpecBuilder() .setBaseUri("http://localhost:8082") .setAccept(ContentType.JSON) .addFilter(new ResponseLoggingFilter()) .addFilter(new RequestLoggingFilter()) .build(); given().spec(spec).when().get("/health/get").then().statusCode(200); } }
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.test</groupId> <artifactId>JettyTestProject</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.6.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>3.0.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.43.v20210629</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.4.43.v20210629</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-servlet-initializer</artifactId> <version>4.7.1.Final</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> </project>
最终解决方案
添加如下依赖即可解决报错:
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson2-provider</artifactId> <version>4.7.1.Final</version> </dependency>
内容的提问来源于stack exchange,提问作者Thomas DK
相关产品推荐
相关产品推荐

