Skip to content

Commit 7dfada9

Browse files
senivamjansupol
authored andcommitted
Spring5/Servlet integration test
Signed-off-by: Maxim Nesen <[email protected]>
1 parent 7fbe085 commit 7dfada9

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2025 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 Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.server.spring.servlet;
18+
19+
import org.glassfish.jersey.server.ResourceConfig;
20+
import org.glassfish.jersey.servlet.ServletContainer;
21+
import org.glassfish.jersey.test.DeploymentContext;
22+
import org.glassfish.jersey.test.JerseyTest;
23+
import org.glassfish.jersey.test.ServletDeploymentContext;
24+
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
25+
import org.glassfish.jersey.test.spi.TestContainerFactory;
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
28+
import org.springframework.web.context.ContextLoaderListener;
29+
30+
import javax.ws.rs.core.Response;
31+
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
34+
/**
35+
* minimalistic test to demonstrate Spring/servlet integration
36+
*/
37+
38+
public class ServletContainerIntegrationTest extends JerseyTest {
39+
40+
@Override
41+
protected DeploymentContext configureDeployment() {
42+
// Set up Spring context
43+
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
44+
context.register(SpringServletService.class);
45+
context.refresh();
46+
47+
final ResourceConfig config = new ResourceConfig(ServletSpringResource.class);
48+
config.property("contextConfig", context);
49+
50+
return ServletDeploymentContext
51+
.builder(config)
52+
.servlet(new ServletContainer(config))
53+
.addListener(ContextLoaderListener.class)
54+
.build();
55+
}
56+
57+
@Override
58+
protected TestContainerFactory getTestContainerFactory() {
59+
// Use Grizzly with servlet support to enable HttpServletRequest
60+
return new GrizzlyWebTestContainerFactory();
61+
}
62+
63+
@Test
64+
public void servletContainerRequestTest() {
65+
Response response = target("/test").request().get();
66+
assertEquals(200, response.getStatus());
67+
}
68+
69+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2025 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 Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.server.spring.servlet;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.ws.rs.GET;
23+
import javax.ws.rs.Path;
24+
import javax.ws.rs.Produces;
25+
import javax.ws.rs.core.Context;
26+
import javax.ws.rs.core.MediaType;
27+
28+
@Path("/test")
29+
public class ServletSpringResource {
30+
31+
@Autowired
32+
private SpringServletService _testService;
33+
34+
@Context
35+
HttpServletRequest request;
36+
37+
@GET
38+
@Produces(MediaType.TEXT_PLAIN)
39+
public String getRequestInfo() {
40+
41+
final String result = _testService.processRequest();
42+
final String clientIP = request.getMethod(); // or any request info
43+
return String.format("Request processed: %s from IP: %s ", result, clientIP);
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 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 Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.server.spring.servlet;
18+
19+
public class SpringServletService {
20+
public String processRequest() {
21+
return "request processed";
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2025 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 Public License v. 2.0, which is available at
8+
http://www.eclipse.org/legal/epl-2.0.
9+
10+
This Source Code may also be made available under the following Secondary
11+
Licenses when the conditions for such availability set forth in the
12+
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
13+
version 2 with the GNU Classpath Exception, which is available at
14+
https://www.gnu.org/software/classpath/license.html.
15+
16+
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17+
18+
-->
19+
20+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans
22+
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop
23+
http://www.springframework.org/schema/aop/spring-aop.xsd">
24+
25+
<bean class="org.glassfish.jersey.server.spring.servlet.SpringServletService"/>
26+
</beans>

0 commit comments

Comments
 (0)