Search code examples
javaweb-servicesrest

Return result of create method and 201 status code


How can i return the result of create method in rest api and also 201 status code? In this code the status code is 200 how can i change it to 201?

    Path("/student")    
    public class MyRestApi{

    @Path("/create")
    public Response create(){
           Student student = new Student;
           //insert in data source
           Return Response.ok(student).build();
          }
}

Solution

  • You can use ResponseBuilder.status(int) OR Response.status(int) method and send it like :-

    Response.ok(student).status(201).build(); // 201 is the response code
    

    OR

    Response.status(201).ok(student).build(); // 201 is the response code