我试图让JSR-311 plugin与Grails 2.3.7一起使用。我使用的版本是0.10,因为我认为0.11需要Grails 2.4。
我已经使用generate-resource命令为域类Product创建了一个端点。创建了两个资源类ProductCollectionResource和ProductResource。我对它们进行了一些微调,但实际上它们看起来像这样:
产品集合资源
@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)
}
}
readAll中的
ProductCollectionResource方法可以正常工作-当我点击它时,我会返回产品列表,但是当我尝试通过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 Angular 来看,看看你在这里得到了什么
@Path('{id}')
@GET
ProductResource getResource(@PathParam('id') Long id) {
这是资源方法(端点)。因此,
ProductsResource将被视为响应主体,就像其他任何资源方法一样。
您似乎正在尝试使用子资源定位器功能,将其转发到
ProductsResource类。为此,子资源定位器(
getResource)应该没有
@HttpMethod批注。这是使资源方法与子资源定位器区分开的因素之一。
因此,只需从
@GET方法中删除
getResource。
@GET已通过
read()中的
ProductsResource方法建立,这将被称为。




