当 JAX-RS 带注释的服务支持完整 CRUD 时,在调用期间出现错误
我遇到了一个相当有趣的阐明错误:
“不允许超过一个 JAX-RS 实体参数(所有其他参数必须使用 JAX-RS 资源参数注释之一进行注释)。”
我正在使用 enunciate 构建 SOAP 和 REST API,支持 XML 和 JSON。 Enunciate 配置为使用所有 service.* 和 service.impl.* 类。每个服务包含用于阐明文档的所有 Javadoc,以及 [@GET @POST @PUT @DELETE] 和 @Path 注释。每个服务实现都包含一个类 @Path 注释。
接口示例:
public interface myService {
@GET
@Path("/something")
Object doGetAll();
@GET
@Path("/something/{id}"
Object doGetOne(@PathParam("id") Integer id);
@PUT
@Path("/something")
Object doCreate(/*params*/);
@POST
@Path("/something/{id}")
Object doUpdate(@PathParam("id") Integer id, /*params*/);
@DELETE
@Path("/something/{id}")
Object doDelete(@PathParam("id") Integer id);
}
实现示例:
@Path("/base")
public class myServiceImpl implements myService {
Object doGetAll() {/*stuff*/}
Object doGetOne(Integer id) {/*stuff*/}
Object doCreate(/*params*/) {/*stuff*/}
Object doUpdate(Integer id, /*params*/) {/*stuff*/}
Object doDelete(Integer id) {/*stuff*/}
}
当我在服务中注释掉“@POST”和“@PUT”注释时,enunciate 将运行得很好。但是,重新发表评论将无法阐明上述消息。问题是我的服务包含 2-8 个不同的模型(例如:我的 addressService 包含 3 个对象:地址、州和国家/地区),因此我指定了路径上下文(使用地址),如下所示:
on the impl:
@Path("/address")
public class myAddressServiceImpl implements myAddressService {}
on方法:
@GET
@Path("/{id}")
findAddressById();
@GET
@Path("/states/{id}")
findStateById();
@GET
@Path("/countries/{id}")
findCountryById();
如果我能提供更多信息,我很乐意。这是一个相当令人沮丧的问题,因为我不确定我是否正确配置了 enunciate(值得怀疑,这是一个基本配置)或者我是否扩展了 JAX-RS 的功能。我花了很多时间阅读教程、谷歌搜索和查看 Javadoc(用于阐明和 JAX-RS),但运气不佳。
还有其他人看到这个问题吗?关于我可以做什么来解决它有什么想法吗?我有一种感觉,这将是那些“不废话”的修复之一……只是无法指出它。
预先感谢您的帮助。
I've run into a rather interesting enunciate error:
"No more than one JAX-RS entity parameter is allowed (all other parameters must be annotated with one of the JAX-RS resource parameter annotations)."
I'm using enunciate to build out both a SOAP and REST API, supporting XML and JSON. Enunciate is configured to use all service.* and service.impl.* classes. Each service contains all Javadoc for the enunciate documentation, as well as a [@GET @POST @PUT @DELETE] and a @Path annotation. Each service impl contains a class @Path annotation.
Example interface:
public interface myService {
@GET
@Path("/something")
Object doGetAll();
@GET
@Path("/something/{id}"
Object doGetOne(@PathParam("id") Integer id);
@PUT
@Path("/something")
Object doCreate(/*params*/);
@POST
@Path("/something/{id}")
Object doUpdate(@PathParam("id") Integer id, /*params*/);
@DELETE
@Path("/something/{id}")
Object doDelete(@PathParam("id") Integer id);
}
Example implementation:
@Path("/base")
public class myServiceImpl implements myService {
Object doGetAll() {/*stuff*/}
Object doGetOne(Integer id) {/*stuff*/}
Object doCreate(/*params*/) {/*stuff*/}
Object doUpdate(Integer id, /*params*/) {/*stuff*/}
Object doDelete(Integer id) {/*stuff*/}
}
When I comment out the "@POST" and "@PUT" annotations in my service, enunciate will run just fine. However, commenting either back in will fail enunciate with the message above. The catch is that I have services that encompass 2-8 different models (example: my addressService incorporates 3 objects: address, state and country), so I've specified the path contexts (using address) as follows:
on the impl:
@Path("/address")
public class myAddressServiceImpl implements myAddressService {}
on the methods:
@GET
@Path("/{id}")
findAddressById();
@GET
@Path("/states/{id}")
findStateById();
@GET
@Path("/countries/{id}")
findCountryById();
If I can provide more information, I'd be happy to. This has been a rather frustrating problem, because I'm not sure if I've configured enunciate incorrectly (doubtful, it's a basic configuration) or if I'm stretching what JAX-RS can do. I've spent a good bit of time reading tutorials, Googling and looking at Javadoc (for enunciate and JAX-RS), but haven't had much luck.
Has anyone else seen this issue? Any thoughts on what I might do to fix it? I'm getting a feeling this will be one of those "no-duh" fixes...just can't put my finger on it.
Thanks in advance for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“实体参数”是由REST请求的主体指定的参数。换句话说,读取请求正文并尝试将其反序列化为参数类型的对象,然后调用请求。
由于只能有一个主体,因此也只能有一个实体参数。
其他参数必须用 @QueryParameter、@PathParameter 等进行注释。
所以这个错误消息真正想说的是“对于所有用 @POST 和 @PUT 注释的方法,你只能有一个参数,而不是所有其他参数都需要有一个参数注释。”
An "entity parameter" is a parameter that is specified by the body of the REST request. In other words, the request body is read and an attempt is made to deserialize it into an an object of your parameter type, and then the request is invoked.
Since there can only be one body, there can only be one entity parameter.
The other parameters have to be annotated with @QueryParameter, @PathParameter, etc.
So what this error message is really trying to say is "for all methods that are annotated with @POST and @PUT, you can only have one parameter that is not annotated with some annotation. All other parameters need to have a parameter annotation."