jersey icon indicating copy to clipboard operation
jersey copied to clipboard

Getter REST method always called

Open robertatgh opened this issue 7 years ago • 0 comments

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:

@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?

robertatgh avatar Dec 24 '17 15:12 robertatgh