泽西岛子资源定位器错误 [英] Jersey Sub-Resource Locators Error

查看:94
本文介绍了泽西岛子资源定位器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Grails 2.3.7的 JSR-311插件。我使用0.10版本,因为我认为0.11需要Grails 2.4。



我使用了 generate-resource 命令为我的域类 Product 创建一个终点。创建两个资源类, ProductCollectionResource ProductResource 。我调整了一下,但基本上它们看起来像这样:
$ b ProductCollectionResource



(''application / json'])
@Produces(['application / json']
@Consumes(['application / json']) ])
class ProductCollectionResource {
$ b $ def productResourceService

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

$ GET
响应readAll(@QueryParam( max)Integer max,@QueryParam(offset)整数偏移){
ok productResourceService.readAll(max,offset)
}
}

ProductResource

  @Consumes(['application / json'])
@Produces(['application / json'])
class ProductResource {
$ b $ def productResourceService
def id

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

$ b $ readAll 方法在 ProductCollectionResource 正常工作 - 当我打开它时,我得到一个产品列表,但是当我试图通过id访问特定产品时(在 / api / products / 123 ),我得到以下错误:

 由MessageException引发:Java类com.myapp.ProductResource的消息正文写入程序和未找到Java类型com.myapp.ProductResource和MIME媒体类型application / json 
- >> 285 |写入com.sun.jersey.spi.container.ContainerResponse
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1479 | _handleRequest in com.sun.jersey.server.impl.application.WebApplicationImpl
| 1391 | handleRequest。 。在''
|中1381 | '
|中的handleRequest 416 |服务。 。 。 。 。在com.sun.jersey.spi.container.servlet.WebComponent
|中538 |服务在com.sun.jersey.spi.container.servlet.ServletContainer
|中716 |服务。 。 。 。 。在''
|中193 | process in org.grails.jaxrs.web.JaxrsContext $ JaxrsServiceImpl
| 45 |手柄。 。 。 。 。 。在org.grails.jaxrs.JaxrsController
|中195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter。 。 。 。 。在grails.plugin.cache.web.filter.AbstractFilter
|中150 |在net.bull.javamelody.JspWrapper
|中调用285 |调用。 。 。 。 。 。在net.bull.javamelody.JdbcWrapper $ DelegatingInvocationHandler
| 198 |在net.bull.javamelody.MonitoringFilter
|中执行doFilter 176 | doFilter。 。 。 。 。在''
|中67 | doFilter。 。 。 。 。在''
|中53 | doFilter in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
| 82 | doFilter in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
| 63 | doFilter。 。 。 。 。在com.odobo.grails.plugin.springsecurity.rest.RestLogoutFilter
| 46 | org.grails.jaxrs.web.JaxrsFilter中的doFilterInternal
| 82 | doFilter。 。 。 。 。在com.brandseye.cors.CorsFilter
|中1145 | java.util.concurrent.ThreadPoolExecutor中的runWorker
| 615 |跑 。 。 。 。 。 。 。在java.util.concurrent.ThreadPoolExecutor $ Worker
^ 744 |在java.lang.Thread中运行

所以它看起来像是试图编组我的 ProductResource 类到JSON,我认为这并不是我想要的。我认为它应该调用 ProductResource.read()方法,并将返回值从该方法编组为JSON。 h2_lin>解决方案

我对Grails没有任何经验,但是从纯粹的泽西岛的角度来看,你看看你在这里得到什么



<$ p $ ($ {
$ {
$ {$ b $ {
$ c>

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



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



因此,只需删除 @GET 来自 getResource 方法。 @GET 已由 ProductsResource read()方法建立c $ c>,这就是所谓的。


I'm trying to get the JSR-311 plugin working with Grails 2.3.7. I'm using version 0.10, because I think 0.11 requires Grails 2.4.

I've used the generate-resource command to create an end point for my domain class Product. Two resource classes are created, ProductCollectionResource and ProductResource. I've tweaked them a bit, but essentially they look like this:

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

The readAll method in ProductCollectionResource works fine - when I hit it, I get back a list of products, however when I try to access a specific product by id (on /api/products/123), I get the following error:

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

So it looks like it's trying to marshall my ProductResource class to JSON, which I think isn't really what I want. I think it should be invoking the ProductResource.read() method, and marshalling the returned value from that method to JSON.

解决方案

I have no experience in Grails whatsoever, but from a pure Jersey standpoint, look at what you got here

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

This is resource method (endpoint). So the ProductsResource will be treated as the response body, just like any other resource method.

You seem to be trying to use the sub-resource locator functionality, forwarding to the ProductsResource class. For that to work, the sub-resource locator (getResource) should not have an @HttpMethod annotation. That's one of the factors that differentiates a resource method from a sub-resource locator.

So just remove the @GET from the getResource method. The @GET is already established by the read() method in the ProductsResource, which is what will be called.

这篇关于泽西岛子资源定位器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆