Skip to content

Commit e87535b

Browse files
committed
Created an example with Jakarta REST 3.1 SeBootstrap & Multipart
Signed-off-by: jansupol <jan.supol@oracle.com>
1 parent e70873a commit e87535b

File tree

9 files changed

+431
-10
lines changed

9 files changed

+431
-10
lines changed
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[//]: # " Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved. "
1+
[//]: # " Copyright (c) 2015, 2023 Oracle and/or its affiliates. All rights reserved. "
22
[//]: # " "
33
[//]: # " This program and the accompanying materials are made available under the "
44
[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at "
@@ -10,20 +10,23 @@ Multipart Web app Example
1010
=========================
1111

1212
This example demonstrates how to develop RESTful web service with
13-
demonstrating JAX-RS Integration with MIME MultiPart Message Formats and
14-
an Jakarta EE 9 compliant Web container.
13+
demonstrating Jakarta-REST Integration with MIME MultiPart Message Formats and
14+
a Jakarta EE 9 compliant Web container.
15+
16+
Please see also comparable example demonstrating usage of Jakarta REST 3.1 API for
17+
MIME MultiPart Message Formats (JERSEY_ROOT/examples/rest31-sebootstrap-multipart)
1518

1619
Contents
1720
--------
1821

1922
The mapping of the URI path space is presented in the following table:
2023

21-
URI path | Description | Sample request using curl
22-
---------------------------- | ---------------------------------------------- | -----------------------------------------------------------------------------------------------
23-
**_/form/part_** | POST message returning entire string | `curl -X POST -F "part=part1" http://localhost:8080/multipart-webapp/form/part`
24-
**_/form/part-file-name_** | POST message returning part filename string. | Be sure to execute this curl from project directory where pom.xml resides
25-
| | `curl -X POST -F "part=@pom.xml" http://localhost:8080/multipart-webapp/form/part-file-name`
26-
**_/form/xml-jaxb-part_** | POST message returning xml jaxb part string. | No curl sample available, please check test sources.
24+
URI path | Description | Sample request using curl
25+
----------------------------|----------------------------------------------| -----------------------------------------------------------------------------------------------
26+
**_/form/part_** | POST message returning entire string | `curl -X POST -F "part=part1" http://localhost:8080/multipart-webapp/form/part`
27+
**_/form/part-file-name_** | POST message returning part filename string. | Be sure to execute this curl from project directory where pom.xml resides
28+
| | | `curl -X POST -F "part=@pom.xml" http://localhost:8080/multipart-webapp/form/part-file-name`
29+
**_/form/xml-jaxb-part_** | POST message returning xml jaxb part string. | No curl sample available, please check test sources.
2730

2831
Running the Example
2932
-------------------
@@ -32,4 +35,4 @@ You can run the example using Jetty as follows:
3235

3336
> `mvn clean package jetty:run`
3437
35-
Following steps are using [cURL](http://curl.haxx.se/) command line tool:
38+
The sample requests are using [cURL](http://curl.haxx.se/) command line tool.

examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<module>oauth-client-twitter</module>
106106
<!--<module>oauth2-client-google-webapp</module>-->
107107
<module>reload</module>
108+
<module>rest31-sebootstrap-multipart</module>
108109
<module>rx-client-webapp</module>
109110
<module>server-async</module>
110111
<module>server-async-managed</module>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[//]: # " Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. "
2+
[//]: # " "
3+
[//]: # " This program and the accompanying materials are made available under the "
4+
[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at "
5+
[//]: # " http://www.eclipse.org/org/documents/edl-v10.php. "
6+
[//]: # " "
7+
[//]: # " SPDX-License-Identifier: BSD-3-Clause "
8+
9+
Multipart Web app Example
10+
=========================
11+
12+
This example demonstrates how to develop RESTful web service with
13+
demonstrating Jakarta REST Integration with MIME MultiPart Message Formats and
14+
Jakarta REST EE 10 SeBootstrap functionality.
15+
16+
Feel free to compare with pre-Jakarta REST 3.1 Jersey Multipart example (JERSEY_ROOT/examples/multipart-webapp).
17+
18+
Contents
19+
--------
20+
21+
The mapping of the URI path space is presented in the following table:
22+
23+
URI path | Description | Sample request using curl
24+
----------------------------|----------------------------------------------| -----------------------------------------------------------------------------------------------
25+
**_/form/part_** | POST message returning entire string | `curl -X POST -F "part=part1" http://localhost:8080/multipart-webapp/form/part`
26+
**_/form/part-file-name_** | POST message returning part filename string. | Be sure to execute this curl from project directory where pom.xml resides
27+
| | | `curl -X POST -F "part=@pom.xml" http://localhost:8080/multipart-webapp/form/part-file-name`
28+
**_/form/xml-jaxb-part_** | POST message returning xml jaxb part string. | No curl sample available, please check test sources.
29+
30+
Running the Example
31+
-------------------
32+
33+
You can run the example using Jetty as follows:
34+
35+
> `mvn clean package exec:java`
36+
37+
The sample requests are using [cURL](http://curl.haxx.se/) command line tool.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Distribution License v. 1.0, which is available at
8+
http://www.eclipse.org/org/documents/edl-v10.php.
9+
10+
SPDX-License-Identifier: BSD-3-Clause
11+
12+
-->
13+
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
15+
16+
<modelVersion>4.0.0</modelVersion>
17+
18+
<parent>
19+
<groupId>org.glassfish.jersey.examples</groupId>
20+
<artifactId>project</artifactId>
21+
<version>3.1.99-SNAPSHOT</version>
22+
</parent>
23+
24+
<artifactId>rest31-sebootstrap-multipart</artifactId>
25+
<name>jersey-examples-multipart-webapp</name>
26+
<packaging>jar</packaging>
27+
28+
<description>Jersey SeBootstrap Multipart example.</description>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.glassfish.jersey.media</groupId>
33+
<artifactId>jersey-media-multipart</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.glassfish.jersey.core</groupId>
37+
<artifactId>jersey-server</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.glassfish.jersey.inject</groupId>
41+
<artifactId>jersey-hk2</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>jakarta.xml.bind</groupId>
45+
<artifactId>jakarta.xml.bind-api</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.sun.xml.bind</groupId>
49+
<artifactId>jaxb-osgi</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.glassfish.jersey.containers</groupId>
53+
<artifactId>jersey-container-jdk-http</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.glassfish.jersey.test-framework</groupId>
57+
<artifactId>jersey-test-framework-core</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.glassfish.jersey.test-framework</groupId>
62+
<artifactId>jersey-test-framework-util</artifactId>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.junit.jupiter</groupId>
67+
<artifactId>junit-jupiter</artifactId>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
72+
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
77+
<build>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.codehaus.mojo</groupId>
81+
<artifactId>exec-maven-plugin</artifactId>
82+
<configuration>
83+
<mainClass>org.glassfish.jersey.examples.multipart.webapp.Main</mainClass>
84+
</configuration>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
<profiles>
90+
<profile>
91+
<id>pre-release</id>
92+
<build>
93+
<plugins>
94+
<plugin>
95+
<groupId>org.codehaus.mojo</groupId>
96+
<artifactId>xml-maven-plugin</artifactId>
97+
</plugin>
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-assembly-plugin</artifactId>
101+
</plugin>
102+
</plugins>
103+
</build>
104+
</profile>
105+
</profiles>
106+
107+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.multipart.webapp;
12+
13+
import jakarta.xml.bind.annotation.XmlRootElement;
14+
15+
@XmlRootElement
16+
public class Bean {
17+
18+
public String value;
19+
20+
public Bean() {
21+
}
22+
23+
public Bean(String str) {
24+
value = str;
25+
}
26+
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.multipart.webapp;
12+
13+
import jakarta.ws.rs.SeBootstrap;
14+
15+
import java.io.IOException;
16+
import java.util.concurrent.ExecutionException;
17+
18+
public class Main {
19+
public static void main(String[] args) throws ExecutionException, InterruptedException {
20+
final SeBootstrap.Configuration.Builder bootstrapConfigurationBuilder = SeBootstrap.Configuration.builder();
21+
bootstrapConfigurationBuilder.property(SeBootstrap.Configuration.PORT, 8080);
22+
23+
SeBootstrap.start(new MyApplication(), bootstrapConfigurationBuilder.build())
24+
.whenComplete((instance1, throwable) -> {
25+
try {
26+
System.out.println("Press enter to exit");
27+
System.in.read();
28+
} catch (IOException e) {
29+
throw new RuntimeException(e);
30+
}
31+
}).thenAccept((i) -> i.stop())
32+
33+
.toCompletableFuture().get();
34+
35+
System.out.println("Exiting...");
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.multipart.webapp;
12+
13+
import jakarta.ws.rs.Consumes;
14+
import jakarta.ws.rs.FormParam;
15+
import jakarta.ws.rs.GET;
16+
import jakarta.ws.rs.POST;
17+
import jakarta.ws.rs.Path;
18+
import jakarta.ws.rs.core.EntityPart;
19+
import jakarta.ws.rs.core.MediaType;
20+
21+
import java.io.IOException;
22+
23+
@Path("/form")
24+
public class MultiPartResource {
25+
26+
@GET
27+
@Path("test")
28+
public String test() {
29+
return "Test successful";
30+
}
31+
32+
@POST
33+
@Path("part")
34+
@Consumes(MediaType.MULTIPART_FORM_DATA)
35+
public String post(@FormParam("part") String s) {
36+
return s;
37+
}
38+
39+
@POST
40+
@Path("part-file-name")
41+
@Consumes(MediaType.MULTIPART_FORM_DATA)
42+
public String post(@FormParam("part")EntityPart entityPart) throws IOException {
43+
return entityPart.getContent(String.class) + ":" + entityPart.getFileName().get();
44+
}
45+
46+
@POST
47+
@Path("xml-jaxb-part")
48+
@Consumes(MediaType.MULTIPART_FORM_DATA)
49+
public String post(
50+
@FormParam("string") EntityPart stringEntityPart,
51+
@FormParam("bean") EntityPart beanEntityPart) throws IOException {
52+
return stringEntityPart.getContent(String.class) + ":" + stringEntityPart.getFileName().get() + ","
53+
+ beanEntityPart.getContent(Bean.class).value + ":" + beanEntityPart.getFileName().get();
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.multipart.webapp;
12+
13+
import jakarta.ws.rs.ApplicationPath;
14+
15+
import org.glassfish.jersey.server.ResourceConfig;
16+
17+
@ApplicationPath("/multipart-webapp")
18+
public class MyApplication extends ResourceConfig {
19+
20+
public MyApplication() {
21+
super(MultiPartResource.class);
22+
}
23+
}

0 commit comments

Comments
 (0)