问题
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.
回答1:
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.
- For more information, see the documentation for Sub-Resource Locators
来源:https://stackoverflow.com/questions/30275284/jersey-sub-resource-locators-error