Sub topic 2
Describing endpoints with @HttpExchange
This chapter assumes you know Java 17 records and interfaces, basic Spring Boot beans, HTTP methods and status codes, and Spring MVC annotations such as @PathVariable. You will learn how annotations turn an interface method into a description of one HTTP request.
The annotation family
@HttpExchange marks an interface or a method as a description of an HTTP exchange. It lives in org.springframework.web.service.annotation. Spring does not read the method body, because an interface has none. It reads the annotations and builds the request from them.
Each HTTP method has a shortcut annotation. The shortcut is @HttpExchange with the method attribute already filled in.
| Annotation | HTTP method |
|---|---|
| `@GetExchange` | GET |
| `@PostExchange` | POST |
| `@PutExchange` | PUT |
| `@PatchExchange` | PATCH |
| `@DeleteExchange` | DELETE |
Use the shortcuts for readability. Use plain @HttpExchange(method = "HEAD") for methods that have no shortcut.
Attributes
All of these annotations share the same attributes:
url(alsovalue): the path, which may contain{placeholders}.method: the HTTP method. The shortcuts set it for you.contentType: theContent-Typeheader to send with the request body.accept: an array of media types for theAcceptheader.
Type-level and method-level together
An @HttpExchange on the interface supplies shared values. Method annotations add to them. The type-level url is prefixed to each method url, and the method's own attributes fill in the rest.
The base URL is not part of the interface. You set it later on the RestClient, which keeps the interface independent of the environment.
A fuller example
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.DeleteExchange;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.annotation.PutExchange;
@HttpExchange(url = "/todos", accept = "application/json")
public interface TodoAdminClient {
@GetExchange("/{id}")
Todo getTodo(@PathVariable long id);
@PutExchange(url = "/{id}", contentType = "application/json")
void replace(@PathVariable long id, @RequestBody Todo todo);
@DeleteExchange("/{id}")
void delete(@PathVariable long id);
@HttpExchange(method = "HEAD", url = "/{id}")
ResponseEntity<Void> exists(@PathVariable long id);
}
record Todo(Long id, Long userId, String title, boolean completed) {}Every method inherits accept = "application/json" from the interface. replace also declares its own contentType. exists uses the general annotation because HEAD has no shortcut.
Common errors
- Missing method annotation. A method with no exchange annotation is not an endpoint. Creating the proxy fails with an
IllegalStateExceptionthat names the method. Annotate every abstract method. - Placeholder and parameter names differ. In
/{id}, the argument must be calledid, or you must write@PathVariable("id"). Without the-parameterscompiler flag, Spring cannot read argument names. Spring Boot's Maven and Gradle plugins enable this flag by default. - Doubled or missing slashes. Write the type-level path as
/todosand method paths as/{id}. Both start with a slash, and neither ends with one. - Setting the method twice.
@HttpExchange(method = "POST")on a@GetExchangemethod is a conflict. Pick one form per method.
Exercise
Exercise
An interface has @HttpExchange("/users") at type level and @GetExchange("/{id}/posts") on a method List<Post> posts(@PathVariable long id). The client's base URL is https://api.example.com. Which request does posts(7) send?
Show answer
A GET request to https://api.example.com/users/7/posts. The type-level /users is prefixed to the method path /{id}/posts, {id} is filled with 7, and the base URL from the client comes first.