Consider the following example:
create new user
POST request for this url : www.example.com/accounts/checking/users
GET user detail
GET request for this url : www.example.com/accounts/checking/user/1
return resource for user with id 1.
Now my question is when I post to www.example.com/accounts/checking/users for a new user creation, a new user is created and its resource uri must be returned in the location header. I am having trouble building this uri using getAbsoluteURIbuilder method.
Here is what I have
@Path("/accounts)
public class AccountResourceService {
@Context
UriInfo uriInfo
//here 'type' can be checking or saving account
@GET
@PATH("{type}/user/{id}")
@Produces(MediaType.APPLICATION_JSON)
public class getUserInfo(final @PathParam("type") String type, @PathParam("id"), int id)
{
//return user-specific resource for the given id
}
@POST
@PATH("{type}/users")
@Produces(MediaType.APPLICATION_JSON)
public class CreateUser(final @PathParam("type") String type, AccountCreateRequest request)
{
if type.equals("checking"){
User user = createAccount(request);
URI uri = uriInfo.getAbsolutePathBuilder().path(user.getId().toString()).build();
System.out.println("created uri is " + uri.toString);
return Response.created(uri).build();
}
else {
//do something else
}
}
}
The uri I am returning in POST method above is
http://localhost:8080/accounts/checking/users/1 //note users
But the expected uri is
http://localhost:8080/accounts/checking/user/1 //not user
how can I get this to work?