org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:找不到类型为

时间:2016-09-03 10:34:08

标签: java jackson resteasy undertow

我使用underow + 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();
    }

1 个答案:

答案 0 :(得分:0)

尝试更改上一个代码spinet,如下所示,因为需要匿名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