如何使用表单参数创建/构建REST URI [英] How to create/build REST URI using form parameters

查看:114
本文介绍了如何使用表单参数创建/构建REST URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我创建了一个宁静的webservice.now,如何为用户"abc"创建路径.
像这样

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

以下是我获取用户输入的HTML表单

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);
    }

如果用户与"abc"具有相同的名称,如何从表单参数中生成类似URI的内容

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

http://mysite/user/abc  

推荐答案

使用@Path@PathParam批注:

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

如果PUT/user/joe的正文为s3cret,则uname将为joe,而password将为s3cret.

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

我使用PUT是因为您要使用的URL暗示用户名是由客户端设置的. /user是所有用户的收集资源.

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.

编辑:由于此方法将通过创建新用户来更改服务器状态,因此必须使用PUTPOST. GET 不得更改服务器状态.

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.

这篇关于如何使用表单参数创建/构建REST URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆