【问题标题】:org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of typeorg.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:找不到响应对象类型的 MessageBodyWriter
【发布时间】:2017-01-11 08:52:02
【问题描述】:

我使用 undertow+resteasy+ajckson 开发 REST 服务,当我使用 IDEA 运行它时,一切正常,但是当我使用 gradle 或 maven 创建“fat-jar”时,我在 GET 查询期间出现错误:

org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: 不能 为类型的响应对象查找 MessageBodyWriter 在 org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:67) 在 org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:448) 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:397) 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:200) 在 org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) 在 org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) 在 org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

我可以使用gradle build 创建没有错误的 jar 并在 IDEA 中调试我的服务。

我的 gradle 文件:

apply plugin: 'application'
apply plugin: 'java'

sourceCompatibility = 1.8
targetCompatibility = 1.8

mainClassName = 'example.json.RestServer'

jar {
    manifest {
        attributes 'Implementation-Title': 'Rest-server',
                'Implementation-Version': '0.1',
                'Main-Class': 'example.json.RestServer'
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

repositories {
    jcenter()
}

dependencies {
    compile 'io.undertow:undertow-core:1.4.0.Final'
    compile 'io.undertow:undertow-servlet:1.4.0.Final'

    compile 'org.jboss.resteasy:jaxrs-api:3.0.12.Final'
    compile 'org.jboss.resteasy:resteasy-undertow:3.0.12.Final'
    compile 'org.jboss.resteasy:resteasy-jackson-provider:3.0.12.Final'

    testCompile 'junit:junit:4.12'
}

我的来源:

@Path("calculator")
public class Calculator {
    @GET
    @Path("squareRoot")
    @Produces(MediaType.APPLICATION_JSON)
    public Response squareRoot(@QueryParam("input") double input){
        Result result = new Result("Square Root");
        result.setInput(input);
        result.setOutput(Math.sqrt(result.getInput()));
        return Response.status(200).entity(result).build();
    }

【问题讨论】:

    标签: java jackson resteasy undertow


    【解决方案1】:

    尝试像下面这样更改您的最后一个代码尖晶石,因为匿名 GenericEntity 子类需要为编写器提供正确的类型信息(否则会被编译器删除)。

    @Path("calculator")
        public class Calculator {
            @GET
            @Path("squareRoot")
            @Produces(MediaType.APPLICATION_JSON)
            public Response squareRoot(@QueryParam("input") double input){
                Result result = new Result("Square Root");
                result.setInput(input);
                result.setOutput(Math.sqrt(result.getInput()));
        return Response.ok(
                new GenericEntity<Result >(result)
                 )
            }
    

    更多解释在这里JAX-RS NoMessageBodyWriterFoundFailure

    【讨论】:

    • 我的代码一切正常,构建 jar 文件时出错,但找不到位置
    猜你喜欢
    • 2013-04-16
    • 2013-11-02
    • 1970-01-01
    • 2012-08-21
    • 2013-10-06
    • 2013-03-15
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    相关资源
    最近更新 更多