我试图让JSR-311 plugin使用Grails 2.3.7。我使用版本0.10,因为我认为0.11需要Grails 2.4。
我已使用generate-resource
命令为我的域类Product
创建终点。创建了两个资源类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)
}
}
ProductResource
@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。
答案 0 :(得分:2)
我没有任何Grails的经验,但从纯粹的泽西观点来看,看看你在这里得到了什么
@Path('{id}')
@GET
ProductResource getResource(@PathParam('id') Long id) {
这是资源方法(端点)。因此,ProductsResource
将被视为响应主体,就像任何其他资源方法一样。
您似乎正在尝试使用子资源定位器功能,转发到ProductsResource
类。为此,子资源定位器(getResource
)不应具有@HttpMethod
注释。这是区分资源方法和子资源定位器的因素之一。
所以只需从@GET
方法中删除getResource
即可。 @GET
已经通过read()
中的ProductsResource
方法建立了<!DOCTYPE html>
...
var color = new Array('#FFF8DC', '#DC143C', '#00FFFF', '#00008B', '#008B8B', '#B8860B', '#A9A9A9', '#006400', '#BDB76B', '#8B008B');
function mark(n0)
{
do
{
n = Math.floor(Math.random()*11)%10
}
while n == n0;
return n;
}
if(window.DeviceMotionEvent) {
var speed = 15;
var x = y = z = x0 = y0 = z0 = n0 = 0;
window.addEventListener('motionListener', function(){
var acceleration =event.accelerationIncludingGravity;
x = acceleration.x;
y = acceleration.y;
n = mark(n0)
if(Math.abs(x-x0) > speed || Math.abs(y-y0) > speed) {
document.body.style.backgroundColor = color[n];
}
x0 = x;
y0 = y;
n0 = n;
}, false);
}
..
,这将被称为。