How to create/build REST URI using form parameters

2020-05-03 10:05发布

okay so i have created restful webservice.now how to create a path for a user "abc".
Something like this

http://stackoverflow.com/user/abc

Following is my form for getting the user input thorugh html

@POST
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void newUser(
            @FormParam("uname") String uname,
            @FormParam("password") String password,
            @Context HttpServletResponse servletResponse
    ) throws IOException {
        User u = new User(uname,password);
        User.userdata.put(uname,password);
    }

How to make a URI something like this from the form parameters if user has uname as "abc"

http://mysite/user/abc  

标签: rest jersey uri
1条回答
不美不萌又怎样
2楼-- · 2020-05-03 10:42

Use the @Path and @PathParam annotations:

@Path("/user/{uname}")
@PUT
@Consumes("text/plain")
public void putUser(@PathParam("uname") String uname, String password) {
  // ..
}

If you PUT to /user/joe with a body of s3cret, then uname will be joe and password will be s3cret.

I use PUT because the URL you want to use implies that the username is set by the client. /user is the collection resource of all users.

Edit: Since this method will change the server state by creating a new user, PUT or POST must be used. GET must not change the server state.

查看更多
登录 后发表回答