14203

Designing REST resource to support multiple methods which produces and consumes exactly the same mim

Question:

I have 3 methods in ProfileResource:

<pre class="lang-java prettyprint-override">@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); }

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.

<pre class="lang-none prettyprint-override">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]

How to resolve this issue?

Answer1:

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.

<blockquote>

A resource model has ambiguous (sub-)resource method for HTTP method GET

</blockquote>

provide different paths for both of them, you can do that using @path() annotation.

Recommend

  • How do I test a Table-Valued Function in SQL Server Management Studio?
  • How to Output the results of a MySQL query that used aliases?
  • How to handle Service unavailable scenarios with Jersey REST
  • Pure JerseyTest without letting Spring messing with services
  • Sending Text Message using JMS on glassfish server
  • Gradle Build Failed For Task app:processReleaseResources
  • Java EE 6 Login module
  • Jersey REST api multiple resources
  • How do I add a File Type Association in a Windows Phone 8.1 app manifest?
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Embedded Glassfish JPA Datasource connection fail
  • Issue with routerLink directive
  • How Get arguments value using inline assembly in C without Glibc?
  • Multiple producers single consumer locking schema
  • Java color detection
  • Access Android Market through SSH tunnel
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • OOP Javascript - Is “get property” method necessary?
  • How to get Eclipse Oxygen to run on Java 9
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Read a local file using javascript
  • ImageMagick, replace semi-transparent white with opaque white
  • Scrapy recursive link crawler
  • Cannot connect to cassandra from Spark
  • Accessing IRQ description array within a module and displaying action names
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Cross-Platform Protobuf Serialization
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Build own AppleScript numerical error handling
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Google cloud sdk not working when python points python3
  • Is there a mandatory requirement to switch app.yaml?
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass