Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Request parameters

Almost every API method needs data from the request: the post number from the address, the page number, a form field. In Jakarta REST you do not dig them out of a request object — you declare a method argument with the right annotation, and the server passes the value, already converted to the needed type.

Part of the path

The variable part of the address goes into @Path in curly braces, and it reaches the method through @PathParam. We continue the file src/main/java/com/example/blog/PostResource.java:

// src/main/java/com/example/blog/PostResource.java (fragment)
@GET
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String show(@PathParam("id") long id) {
    return "Post #" + id;
}
 
@GET
@Path("/{id}/comments/{commentId}")
@Produces(MediaType.TEXT_PLAIN)
public String comment(@PathParam("id") long id, @PathParam("commentId") long commentId) {
    return "Comment " + commentId + " on post " + id;
}

The string from the address becomes a long automatically. Let's call both methods:

curl http://localhost:8080/blog/api/posts/5
curl http://localhost:8080/blog/api/posts/5/comments/12
Post #5
Comment 12 on post 5

Query string

Parameters after ? are read by @QueryParam. If a parameter may be missing, @DefaultValue sets the default:

// src/main/java/com/example/blog/PostResource.java (fragment)
@GET
@Produces(MediaType.TEXT_PLAIN)
public String list(@QueryParam("page") @DefaultValue("1") int page,
                   @QueryParam("tag") String tag) {
    return "Posts, page " + page + ", tag: " + tag;
}
curl http://localhost:8080/blog/api/posts
curl "http://localhost:8080/blog/api/posts?page=2&tag=java"
Posts, page 1, tag: null
Posts, page 2, tag: java

Without @DefaultValue a missing parameter gives null for objects and 0 for numbers.

Headers and form fields

// src/main/java/com/example/blog/PostResource.java (fragment)
@GET
@Path("/agent")
@Produces(MediaType.TEXT_PLAIN)
public String agent(@HeaderParam("User-Agent") String userAgent) {
    return "Your client: " + userAgent;
}
 
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public String create(@FormParam("title") String title) {
    return "Created a post: " + title;
}

@Consumes says what body type the method accepts: here it is a regular HTML form.

curl http://localhost:8080/blog/api/posts/agent
curl -d "title=Hello" http://localhost:8080/blog/api/posts
Your client: curl/8.21.0
Created a post: Hello

For text outside plain ASCII, send the header Content-Type: application/x-www-form-urlencoded; charset=UTF-8: without charset Open Liberty reads form fields as ISO-8859-1, and letters such as é or Cyrillic arrive garbled.

The address /posts/agent also matches the /{id} template, but the exact path wins: Jakarta REST prefers the method with the more specific @Path.

Summary

AnnotationWhere the value comes fromExample
@PathParamPart of the path/posts/5
@QueryParamQuery string/posts?page=2
@HeaderParamHeaderUser-Agent: curl
@FormParamForm fieldtitle=Hello
@CookieParamCookietheme=dark
@DefaultValueValue when the parameter is missing—

Common mistake

Passing a path value that cannot be converted to the argument type. For /api/posts/abc the string abc does not become a long, and the server answers not with 400 but with 404 Not Found and an empty body — for Jakarta REST such an address simply does not match the method. If you want a clear 400 response, accept a String and check the value yourself. The validation lesson shows one more way — constraints on parameters.

Thanks for reading!

Donate to the site — we work hard for you.