Json 泽西岛子资源定位器错误
我正在尝试使用Grails2.3.7。我使用的是版本0.10,因为我认为0.11需要Grails2.4 我已经使用Json 泽西岛子资源定位器错误,json,grails,jersey,Json,Grails,Jersey,我正在尝试使用Grails2.3.7。我使用的是版本0.10,因为我认为0.11需要Grails2.4 我已经使用生成资源命令为我的域类产品创建了一个端点。创建了两个资源类,ProductCollectionResource和ProductResource。我对它们进行了一些调整,但本质上它们看起来是这样的: ProductCollectionResource @Path('/api/products') @Consumes(['application/json']) @Produces(['a
生成资源
命令为我的域类产品
创建了一个端点。创建了两个资源类,ProductCollectionResource
和ProductResource
。我对它们进行了一些调整,但本质上它们看起来是这样的:
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)
}
}
@Consumes(['application/json'])
@Produces(['application/json'])
class ProductResource {
def productResourceService
def id
@GET
Response read() {
ok productResourceService.read(id)
}
}
产品资源
@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)
}
}
@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
类打包为JSON,我认为这并不是我真正想要的。我认为它应该调用ProductResource.read()
方法,并将该方法返回的值编组为JSON。我在Grails方面没有任何经验,但从纯Jersey的角度来看,看看你在这里得到了什么
@Path('{id}')
@GET
ProductResource getResource(@PathParam('id') Long id) {
这是资源方法(端点)。因此,ProductsResource
将被视为响应主体,就像任何其他资源方法一样
您似乎试图使用子资源定位器功能,将其转发到ProductsResource
类。为此,子资源定位器(getResource
)不应具有@HttpMethod
注释。这是区分资源方法和子资源定位器的因素之一
因此,只需从getResource
方法中删除@GET
。@GET
已经通过ProductsResource
中的read()
方法建立,这就是将要调用的方法
- 有关更多信息,请参阅的文档