非Spring Boot环境下在Vaadin 23 WAR应用中集成REST API
无需重构为Spring Boot,为Vaadin 23 WAR应用添加REST API的方案
完全可以不用将应用重构为Spring Boot,以下是几种可行的实现方式:
方案1:使用Jakarta EE原生JAX-RS(推荐)
这是Java EE标准的REST实现,无需依赖Spring,Tomcat直接支持,只需添加对应依赖并配置即可。
步骤:
- 添加Maven依赖(以Jersey为例,也可选择RESTEasy等其他实现):
<!-- Jakarta REST API 规范 --> <dependency> <groupId>jakarta.ws.rs</groupId> <artifactId>jakarta.ws.rs-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Jersey 核心容器 --> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>3.1.5</version> </dependency> <!-- Jersey 依赖注入支持 --> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>3.1.5</version> </dependency>
- 创建JAX-RS应用配置类,指定API根路径:
import jakarta.ws.rs.ApplicationPath; import jakarta.ws.rs.core.Application; @ApplicationPath("/api") public class RestApiApplication extends Application { // 空类即可,框架会自动扫描带@Path注解的端点类 }
- 编写REST端点:
import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; @Path("/hello") public class HelloEndpoint { @GET @Produces(MediaType.APPLICATION_JSON) public String getHelloMessage() { return "{\"message\": \"Hello from Vaadin WAR REST API\"}"; } }
部署后,访问路径示例:http://localhost:8080/你的WAR上下文路径/api/hello
方案2:让Spring Web MVC在非Spring Boot环境正常工作
你之前尝试Spring Web MVC无响应,是因为缺少Spring上下文的手动初始化配置(Spring Boot会自动完成这些步骤),只需补充配置即可:
步骤:
- 添加Spring Web MVC依赖:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.27</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.27</version> </dependency>
- 在
web.xml中配置Spring DispatcherServlet:
<servlet> <servlet-name>spring-rest-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-rest-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-rest-dispatcher</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
- 创建
spring-rest-config.xml配置文件,开启组件扫描和MVC注解支持:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描你的@RestController所在的包 --> <context:component-scan base-package="com.yourcompany.rest"/> <!-- 启用Spring MVC注解驱动 --> <mvc:annotation-driven/> </beans>
- 编写@RestController类:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloRestController { @GetMapping("/hello") public String hello() { return "{\"message\": \"Hello from Spring MVC REST API\"}"; } }
部署后访问路径示例:http://localhost:8080/你的WAR上下文路径/api/hello
方案3:使用原生Servlet实现(适合极简API)
如果你的API逻辑非常简单,甚至可以不用任何框架,直接基于Servlet API实现:
步骤:
- 创建Servlet类:
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/api/hello") public class HelloApiServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("application/json"); response.getWriter().write("{\"message\": \"Hello from Native Servlet API\"}"); } }
这个方案无需额外依赖,Tomcat原生支持,部署后直接访问:http://localhost:8080/你的WAR上下文路径/api/hello
内容的提问来源于stack exchange,提问作者Michael Adams
相关产品推荐
相关产品推荐

