eclipse-ee4j / eclipse-ee4j/jersey

Getter REST method always called

Open
#3,743 2 comments 1 reaction 0 assignees View on GitHub
bean-validation
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

When a REST class has a getter that returns a validated object, then it is always called, even if the HTTP method does not match.

Let's say I have the following class:
```java
@Path("/hello")
@ApplicationScoped
public class HelloResource {

private HelloMessage message;

@PostConstruct
public void init() {
message = new HelloMessage();
message.setMessage("Hello world!");
}

@GET
public @Valid HelloMessage getSomething() {
return message;
}

@POST
public @Valid HelloMessage updateMessage(@Valid HelloMessage message) {
this.message = message;
return message;
}

}
```
If I do a POST to /hello, you will see the getSomething method being called, before updateMessage is called.
If I remove the @Valid annotation on the return type of the getSomething method, then getSomething is not called.

Is this according to specifications? Should you basically never name a method starting with "get" in a REST class?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.