泽西岛子资源定位器错误

Jersey Sub-Resource Locators Error

我正在尝试让 JSR-311 plugin 与 Grails 2.3.7 一起工作。我正在使用 0.10 版,因为我认为 0.11 需要 Grails 2.4。

我已经使用 generate-resource 命令为我的域 class Product 创建了一个端点。创建了两个资源 class,ProductCollectionResourceProductResource。我对它们做了一些调整,但基本上它们看起来像这样:

ProductCollectionResource

@Path('/api/products')
@Consumes(['application/json'])
@Produces(['application/json'])
class ProductCollectionResource {

    def productResourceService

    @Path('{id}')
    @GET
    ProductResource getResource(@PathParam('id') Long id) {
        new ProductResource(productResourceService: productResourceService, id:id)
    }

    @GET
    Response readAll(@QueryParam("max") Integer max, @QueryParam("offset") Integer offset) {
        ok productResourceService.readAll(max, offset)
    }
}

ProductResource

@Consumes(['application/json'])
@Produces(['application/json'])
class ProductResource {

    def productResourceService
    def id

    @GET
    Response read() {
        ok productResourceService.read(id)
    }
}

ProductCollectionResource 中的 readAll 方法工作正常 - 当我点击它时,我得到了一个产品列表,但是当我尝试通过 id 访问特定产品时(在 /api/products/123), 我收到以下错误:

Caused by MessageException: A message body writer for Java class com.myapp.ProductResource, and Java type class com.myapp.ProductResource, and MIME media type application/json was not found
->>  285 | write              in com.sun.jersey.spi.container.ContainerResponse
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1479 | _handleRequest     in com.sun.jersey.server.impl.application.WebApplicationImpl
|   1391 | handleRequest . .  in     ''
|   1381 | handleRequest      in     ''
|    416 | service . . . . .  in com.sun.jersey.spi.container.servlet.WebComponent
|    538 | service            in com.sun.jersey.spi.container.servlet.ServletContainer
|    716 | service . . . . .  in     ''
|    193 | process            in org.grails.jaxrs.web.JaxrsContext$JaxrsServiceImpl
|     45 | handle . . . . . . in org.grails.jaxrs.JaxrsController
|    195 | doFilter           in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
|    150 | invoke             in net.bull.javamelody.JspWrapper
|    285 | invoke . . . . . . in net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler
|    198 | doFilter           in net.bull.javamelody.MonitoringFilter
|    176 | doFilter . . . . . in     ''
|     67 | doFilter . . . . . in     ''
|     53 | doFilter           in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     82 | doFilter           in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|     63 | doFilter . . . . . in com.odobo.grails.plugin.springsecurity.rest.RestLogoutFilter
|     46 | doFilterInternal   in org.grails.jaxrs.web.JaxrsFilter
|     82 | doFilter . . . . . in com.brandseye.cors.CorsFilter
|   1145 | runWorker          in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run                in java.lang.Thread

所以看起来它正在尝试将我的 ProductResource class 编组到 JSON,我认为这不是我真正想要的。我认为它应该调用 ProductResource.read() 方法,并将该方法的返回值编组为 JSON.

我没有任何 Grails 经验,但从纯 Jersey 的角度来看,看看你得到了什么

@Path('{id}')
@GET
ProductResource getResource(@PathParam('id') Long id) {

这是资源方法(端点)。所以 ProductsResource 将被视为响应主体,就像任何其他资源方法一样。

您似乎在尝试使用子资源定位器功能,转发到 ProductsResource class。为此,子资源定位器 (getResource) 不应具有 @HttpMethod 注释。这是区分资源方法和子资源定位器的因素之一。

所以只需从 getResource 方法中删除 @GET 即可。 @GET 已经由 ProductsResource 中的 read() 方法建立,这就是将要调用的内容。