JAX-RS @FormParam example

In JAX-RS, you can use @FormParam annotation to bind HTML form parameters value to a Java method. The following example show you how to do it :

1. HTML Form

See a simple HTML form with “post” method.

<html>
<body>
    <h1>JAX-RS @FormQuery Testing</h1>

    <form action="rest/user/add" method="post">
        <p>
            Name : <input type="text" name="name" />
        </p>
        <p>
            Age : <input type="text" name="age" />
        </p>
        <input type="submit" value="Add User" />
    </form>

</body>
</html>

2. @FormParam Example

Example to use @FormParam to get above HTML form parameter values.

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/user")
public class UserService {

    @POST
    @Path("/add")
    public Response addUser(
        @FormParam("name") String name,
        @FormParam("age") int age) {

        return Response.status(200)
            .entity("addUser is called, name : " + name + ", age : " + age)
            .build();

    }

}

3. Demo

Access HTML Page. URL : http://localhost:8080/RESTfulExample/UserForm.html
demo html page

When “add user” button is clicked, it will redirect to URL : http://localhost:8080/RESTfulExample/rest/user/add
demo jax-rs result

and display the following output :

addUser is called, name : mkyong 123, age : 12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值