/java/

JAX-RS Form

2015-08-05 00:07:56

Below you can find four examples how to work with forms.

package pl.btbw;

import javax.ws.rs.*;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

@Path("/")
public class MyCtrl {

    @POST
    @Path("/save/simple")
    public String save(@FormParam("login") String login, @FormParam("password") String password) {
        return String.format("**Login**: %s, **Password**: %s", login, password);
    }

    @POST
    @Path("/save/dto")
    public String save(@BeanParam SimpleForm dto) {
        return String.format("**Login**: %s, **Password**: %s", dto.getLogin(), dto.getPassword());
    }

    @POST
    @Path("/save/multivalue")
    public String save(MultivaluedMap

<string, string=""> form) {
        return String.format("<b>Login</b>: %s, <b>Password</b>: %s", form.get("login"), form.get("password"));
    }

    @POST
    @Path("/save/form")
    public String save(Form form) {
        MultivaluedMap<string, string=""> fomrAsMap = form.asMap();
        return String.format("<b>Login</b>: %s, <b>Password</b>: %s", fomrAsMap.get("login"), fomrAsMap.get("password"));
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String form() {
        return getLoginPage("save/simple") +
                getLoginPage("save/dto") +
                getLoginPage("save/multivalue") +
                getLoginPage("save/form")
                ;
    }

    private String getLoginPage(String url) {
        String form = url + "<form action='" + url + "' method="POST">";
        form += "<input name="login" type="text"/>";
        form += "<input name="password" type="password"/>";
        form += "<button>ok</button>";
        form += "</form>";
        return form;
    }

}
package pl.btbw;

import javax.ws.rs.FormParam;

public class SimpleForm {

    @FormParam("login")
    private String login;

    @FormParam("password")
    private String password;

    public SimpleForm() {
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }
}

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>example</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
</dependency>
</dependencies>
<build>
<finalname>example</finalname>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.7
                    <target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<configuration>
<failonmissingwebxml>false</failonmissingwebxml>
</configuration>
</plugin>
</plugins>
</build>
</project>