【发布时间】:2017-04-06 22:42:14
【问题描述】:
在 Spring Boot 1.5.2 上,我正在缩减一个包含 Web 服务的大型 Web 应用程序,使其仅成为 Jersey Web 服务。因为 Web 服务已经有一套完整的由 Apache Wink 实现的 JAX-RS 注释,所以我决定使用 Spring + Jersey 而不是 Spring Rest。我发现这个spring-boot-jersey-sample 应用程序可以用作参考。我正在开发的应用程序与示例之间的最大区别在于我的端点定义分为接口和实现。
我在 pom.xml 中添加了以下内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
我的新泽西配置如下所示:
package com.example.configuration;
import org.glassfish.jersey.server.ResourceConfig;
import com.example.EndpointImpl;
import org.springframework.stereotype.Component;
@Component
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
registerEndpoints();
}
private void registerEndpoints() {
register(EndpointImpl.class);
}
}
然后我有以下Application.java:
package com.example;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
new Application().configure(new SpringApplicationBuilder(Application.class)).run(args);
}
}
端点被定义为接口和实现,如下所示(减去导入):
public interface Endpoint {
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getHello(@Context ServletContext sc, @Context HttpServletRequest req, @Context HttpHeaders httpHeaders) ;
}
@Path("")
@Component
public class EndpointImpl implements Endpoint {
@Override
public Response getHello(@Context ServletContext sc, @Context HttpServletRequest req,
@Context HttpHeaders httpHeaders) {
return Response.ok("hello").build();
}
}
当我启动我的应用程序时,我看到消息说 Tomcat 已启动,包括一个消息说 Mapping servlet: 'com.example.configuration.JerseyConfiguration' to [/*]。但是,当我使用 Web 浏览器访问 / 时,我收到 404 Not Found 错误。看起来 GET 定义并没有被采纳。
【问题讨论】:
标签: spring spring-boot jersey jax-rs