Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

Chapter 3. Using @Path and @GET, @POST, etc.


@Path("/library")
public class Library {

   @GET
   @Path("/books")
   public String getBooks() {...}

   @GET
   @Path("/book/{isbn}")
   public String getBook(@PathParam("isbn") String id) {
      // search my database and get a string representation and return it
   }

   @PUT
   @Path("/book/{isbn}")
   public void addBook(@PathParam("isbn") String id, @QueryParam("name") String name) {...}

   @DELETE
   @Path("/book/{id}")
   public void removeBook(@PathParam("id") String id {...}

   
}
Copy to Clipboard Toggle word wrap
If you have the RESTEasy Servlet configured and reachable at a root path of http://myhost.com/services, the requests would be handled by the Library class:
  • GET http://myhost.com/services/library/books
  • GET http://myhost.com/services/library/book/333
  • PUT http://myhost.com/services/library/book/333
  • DELETE http://myhost.com/services/library/book/333
The @javax.ws.rs.Path annotation must exist on either the class or a resource method, or both. If it exists on both the class and method, the relative path to the resource method is a concatenation of the class and method.
The @javax.ws.rs package contains annotations for each HTTP method. @GET, @POST, @PUT, @DELETE, and @HEAD. Place these annotations on public methods that you want to map to the annotation's HTTP method. If a @Path annotation exists on the class, you do not need to annotate the method you wish to map with @Path. Multiple HTTP methods can be used, as long as they can be distinguished from other methods.
When a method is annotated with @Path without a HTTP method being applied, the annotated method is referred to as a JAXRSResourceLocator.

3.1. @Path and regular expression mappings

The @Path annotation is not limited to simple path expressions. You can also insert regular expressions into the value of @Path. For example:
@Path("/resources)
public class MyResource {

   @GET
   @Path("{var:.*}/stuff")
   public String get() {...}
}
Copy to Clipboard Toggle word wrap
The following GETs will route to the getResource() method:
GET /resources/stuff
GET /resources/foo/stuff
GET /resources/on/and/on/stuff
Copy to Clipboard Toggle word wrap
The format of the expression is:
"{" variable-name [ ":" regular-expression ] "}"
Copy to Clipboard Toggle word wrap
Here, regular-expression is optional. Where this is not provided, the expression defaults to a wildcard matching of one particular segment, like so:
"([]*)"
Copy to Clipboard Toggle word wrap
For example:
@Path("/resources/{var}/stuff")
Copy to Clipboard Toggle word wrap
will match these:
GET /resources/foo/stuff
GET /resources/bar/stuff
Copy to Clipboard Toggle word wrap
but will not match:
GET /resources/a/bunch/of/stuff
Copy to Clipboard Toggle word wrap
Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat