Add form examples to Jersey modules
This commit is contained in:
parent
760825405a
commit
d20c1616c1
|
@ -76,6 +76,11 @@
|
|||
<artifactId>jersey-apache-connector</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-multipart</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -103,4 +108,4 @@
|
|||
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.jersey.server.form;
|
||||
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.glassfish.jersey.media.multipart.FormDataParam;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Path("form")
|
||||
public class FormExampleResource
|
||||
{
|
||||
@GET
|
||||
@Path("/example1")
|
||||
@Produces({MediaType.TEXT_HTML})
|
||||
public InputStream getExample1() throws Exception
|
||||
{
|
||||
File f = new File("src/main/resources/html/example1.html");
|
||||
return new FileInputStream(f);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/example2")
|
||||
@Produces({MediaType.TEXT_HTML})
|
||||
public InputStream getExample2() throws Exception
|
||||
{
|
||||
File f = new File("src/main/resources/html/example2.html");
|
||||
return new FileInputStream(f);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/example1")
|
||||
public String example1(@FormParam("first_name") String firstName,
|
||||
@FormParam("last_name") String lastName,
|
||||
@FormParam("age") String age)
|
||||
{
|
||||
return "Got: First = " + firstName + ", Last = " + lastName + ", Age = " + age;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/example2")
|
||||
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||
public String example2(@FormDataParam("first_name") String firstName,
|
||||
@FormDataParam("last_name") String lastName,
|
||||
@FormDataParam("age") String age,
|
||||
@FormDataParam("photo") InputStream photo)
|
||||
throws Exception
|
||||
{
|
||||
int len;
|
||||
int size = 1024;
|
||||
byte[] buf;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
buf = new byte[size];
|
||||
while ((len = photo.read(buf, 0, size)) != -1)
|
||||
bos.write(buf, 0, len);
|
||||
buf = bos.toByteArray();
|
||||
return "Got: First = " + firstName + ", Last = " + lastName + ", Age = " + age + ", Photo (# of bytes) = " + buf.length;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Example 1 using @FormParam</title>
|
||||
</head>
|
||||
<body>
|
||||
<form method="post" action="/form/example1">
|
||||
<label for="first_name">First Name</label>
|
||||
<input id="first_name" name="first_name" type="text">
|
||||
<label for="last_name">Last Name</label>
|
||||
<input id="last_name" name="last_name" type="text">
|
||||
<label for="age">Age</label>
|
||||
<input id="age" name="age" type="text">
|
||||
<input type="submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,18 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Example 2 using @FormDataParam</title>
|
||||
</head>
|
||||
<body>
|
||||
<form method="post" action="/form/example2" enctype="multipart/form-data">
|
||||
<label for="first_name">First Name</label>
|
||||
<input id="first_name" name="first_name" type="text">
|
||||
<label for="last_name">Last Name</label>
|
||||
<input id="last_name" name="last_name" type="text">
|
||||
<label for="age">Age</label>
|
||||
<input id="age" name="age" type="text">
|
||||
<label for="photo">Profile Photo</label>
|
||||
<input id="photo" name="photo" type="file">
|
||||
<input type="submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue