Enterprise Java Development@TOPIC@

Chapter 107. JAX-RS Basics

107.1. JAX-RS Client Basics
107.2. JAX-RS Server Basics
107.3. JAX-RS Maven Aspects
107.4. Summary
  1. Declare root URI using class that extends Application

    import javax.ws.rs.ApplicationPath;
    
    import javax.ws.rs.core.Application;
    @ApplicationPath("api")
    public class TodosApplication extends Application {
    }
  2. Declare resource class and its path below root

    import javax.ws.rs.Path;
    
    @Path("greetings")
    
    public class GreetingsResource {
  3. Declare resource method

    import javax.ws.rs.GET;
    
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.Response.ResponseBuilder;
    @GET
    
    @Path("hi")
    @Produces(MediaType.TEXT_PLAIN)
    public Response sayHi() {
       //...
    }
  4. Implement a response

    public Response sayHi() {
    
        String entity = "hi";
        ResponseBuilder rb = Response.ok(entity);            
        return rb.build();
    }