设计REST资源以支持产生和使用完全相同的mime类型的多种方法 [英] Designing REST resource to support multiple methods which produces and consumes exactly the same mime-types
问题描述
我在ProfileResource
中有3种方法:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Profile> getAllProfiles() {
return profileService.getAllProfiles();
}
@GET
@Path("{profileId}")
@Produces(MediaType.APPLICATION_JSON)
public Profile getProfile(@PathParam("profileId") String profileId) {
return profileService.getProfile(profileId);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Profile getProfileByName(@QueryParam("profileName") String profileName) {
return profileService.getProfileByName(profileName);
}
在服务器启动期间发生以下错误,因为getAllProfiles
和getProfileByName
方法都是GET
方法,所以两者都产生MediaType.APPLICATION_JSON
并且它们之间没有Path
差异.
During server startup below error is thrown, since both getAllProfiles
and getProfileByName
methods are GET
methods, both produce MediaType.APPLICATION_JSON
and there is no Path
difference b/n them.
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public restapi.model.Profile restapi.resources.ProfileResource.getProfileByName(java.lang.String) and public java.util.List restapi.resources.ProfileResource.getAllProfiles() at matching regular expression /profiles. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@4e1d247f']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555) ~[jersey-server-2.22.2.jar:na]
如何解决此问题?
推荐答案
除非提供了不同的访问路径,否则getAllProfiles()和getProfileByName()都是有区别的,除非服务器无法区分它们.这样您将获得以下错误.
You have differentiate Both getAllProfiles() and getProfileByName() by providing difference paths to access them, unless that the server can not distinguish them. so that you will get below error.
资源模型具有用于HTTP方法GET的模棱两可的(子)资源方法
A resource model has ambiguous (sub-)resource method for HTTP method GET
为两者提供不同的路径,您可以使用@path()注释来实现.
provide different paths for both of them, you can do that using @path() annotation.
这篇关于设计REST资源以支持产生和使用完全相同的mime类型的多种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!