Merge pull request #14698 from michael-pratt/BAEL-6867

Add form examples to Jersey modules
This commit is contained in:
Vini 2023-10-11 09:33:05 +02:00 committed by GitHub
commit 64377f45a2
4 changed files with 101 additions and 1 deletions

View File

@ -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>
@ -102,4 +107,4 @@
<jersey.version>3.1.1</jersey.version>
</properties>
</project>
</project>

View File

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

View File

@ -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>

View File

@ -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>