使Jersey 2与嵌入式Tomcat兼容的依赖性是什么

问题描述 投票:0回答:1

基于此问题的答案-[How to add a Jersey REST webservice into embedded tomcat?

这似乎对其他人有用。是否有人拥有GIT项目或Maven项目所需的依赖项列表,这些项目将适用于具有嵌入式tomcat(v8.x或更高版本)和Jersey v2.x的REST端点?

这是POM文件

http://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0

<artifactId>unsprung-tomcat-container</artifactId>
<version>0.1.0-beta</version>
<packaging>jar</packaging>

<properties>
    <tomcat.version>8.5.23</tomcat.version>
    <jersey.version>2.26</jersey.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

这里是启动tomcat的班级:

公共类Main {

// constants
private static final String JERSEY_SERVLET_NAME = "jersey-container-servlet";

// Base URI the HTTP server will listen on
public static final String APP_NAME = "api";
public static final String BASE_URI = "http://localhost:8080/"+APP_NAME+"/";

// attributes
private static String baseUrl;

public static Tomcat startServer(boolean useSsl) throws Exception {
    // create server configuration
    final ResourceConfig rc = new ResourceConfig()
                    .packages("cs.toolkit.ms.tomcat")
                    .registerClasses(GsonMessageBodyHandler.class;

    String port = System.getenv("PORT");
    if (port == null || port.isEmpty()) {
        port = "8080";
    }

    String contextPath = "";
    String appBase = ".";

    Tomcat tomcat = new Tomcat();
    tomcat.setPort(Integer.valueOf(port));
    tomcat.getHost().setAppBase(appBase);

    Context context = tomcat.addContext(contextPath, appBase);
    Tomcat.addServlet(context, JERSEY_SERVLET_NAME,
            new ServletContainer(rc));
    context.addServletMappingDecoded("/api/*", JERSEY_SERVLET_NAME);

    tomcat.start();

    return tomcat;
}

}

这里是包含端点的资源类

@Path("/")

公共类MyResource {

/**
 * Method handling HTTP GET requests.The returned object will be sent 
 * to the client as "text/plain" media type.
 *
 * @param requestType - request type URI
 * @return TestPojo object
 */
@GET
@Path("{requestType}")
@Produces(MediaType.APPLICATION_JSON)
public TestPojo getIt(@PathParam("requestType") String requestType) {
    TestPojo pojo = new TestPojo();
    pojo.setName(requestType);

    return pojo;
}

}

...启动服务器后,这里是我用来测试端点的代码

    ClientConfig clientConfig = new ClientConfig();


    Client client = ClientBuilder.newBuilder()
            .withConfig(clientConfig)
            .build();

    client.register(GsonMessageBodyHandler.class);
    target = client.target(Main.BASE_URI);

    String responseMsg = target.path("hello").request().get(String.class);

...这是我得到的错误

javax.ws.rs.NotAuthorizedException:HTTP 401未经授权

基于此问题的答案-[如何将Jersey REST Web服务添加到嵌入式tomcat?这似乎对其他人有用。是否有人有GIT项目或...

rest tomcat embedded jersey-2.0
1个回答
0
投票

我确定了使嵌入式tomcat实例启动并在其余端点上接受请求所需的依赖项。

© www.soinside.com 2019 - 2024. All rights reserved.