Rest 让Jersey 2与嵌入式Tomcat一起工作的依赖性是什么

Rest 让Jersey 2与嵌入式Tomcat一起工作的依赖性是什么,rest,tomcat,embedded,jersey-2.0,Rest,Tomcat,Embedded,Jersey 2.0,根据对这个问题的回答- [ 这似乎适用于其他人。是否有人拥有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

根据对这个问题的回答- [

这似乎适用于其他人。是否有人拥有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>
}

下面是包含端点的资源类

@Path("/")
    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);
公共类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;
}
}

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

@Path("/")
    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 Unauthorized

我找出了让嵌入式tomcat实例启动并接受rest端点上的请求所需的依赖项

pom文件:

`
8.5.47
8.5.2
2.28
2.8.6
UTF-8
`
org.glassfish.jersey.ext
泽西mvc自由标记
${jersey mvc.version}
javax.servlet
servlet api
org.glassfish.jersey.ext
泽西mvc
org.glassfish.jersey.inject
泽西-hk2
${jersey mvc.version}
org.glassfish.jersey.ext
泽西mvc jsp
${jersey mvc.version}
javax.servlet
servlet api
org.apache.tomcat
TomcatServletAPI
${tomcat.version}
org.apache.tomcat.embed
tomcat嵌入式内核
${tomcat.version}
org.apache.tomcat.embed
tomcat嵌入式日志
${tomcat logging.version}
org.apache.tomcat.embed
汤姆卡特·贾斯珀
${tomcat.version}
com.google.code.gson
格森
${json.version}
org.glassfish.jersey.core
泽西岛客户
${jersey mvc.version}
测试
朱尼特
朱尼特
4.12
测试
org.hamcrest
汉克雷斯特岩芯
1.3
测试
主类

公共类主{
//常数
公共静态int端口=8443;
公共静态最终字符串KEYSTORE\u SERVER\u FILE=“KEYSTORE\u SERVER”;
公共静态最终字符串密钥库\u服务器\u PWD=“asdfgh”;
公共静态最终字符串TRUSTORE\u SERVER\u FILE=“truststore\u SERVER”;
公共静态最终字符串TRUSTORE\u SERVER\u PWD=“asdfgh”;
//属性
私有静态Tomcat;
public static void stop()引发LifecycleException{
tomcat.stop();
}
公共静态void start(字符串…args)引发异常{
//设置配置tomcat的系统属性
System.setProperty(“tomcat.util.scan.StandardJarScanFilter.jarsToSkip”,“*.jar”);//跳过TLD/JSP扫描
//创建服务器实例
tomcat=新的tomcat();
//设置tomcat端口形式的yml配置文件
tomcat.setPort(端口);
System.out.println(“Tomcat在端口上运行:“+端口”);
//设置SSL连接器
连接器httpsConnector=getSslConnector();
tomcat.getService().addConnector(httpsConnector);
tomcat.setConnector(httpsConnector);
//创建web应用程序
文件库=新文件(“src/main/webapp”);
Context-Context=tomcat.addContext(“/api”,base.getAbsolutePath());
tomcat.addWebapp(null,“,base.getAbsolutePath());
System.out.println(“WEB-URI基本路径:+base.getAbsolutePath());
addServlet(上下文,“jersey容器servlet”,resourceConfig());
addServletMapping(“/*”,“jersey容器servlet”);
//启动服务器
tomcat.start();
}
专用静态连接器getSslConnector(){
路径ksfPath=FileSystems.getDefault().getPath(密钥库\服务器\文件);
Path tsfPath=FileSystems.getDefault().getPath(信任服务器文件);
连接器=新连接器();
连接器。设置端口(端口);
连接器。设置安全(正确);
连接器。设置模式(“https”);
setAttribute(“keystoreType”、“JKS”);
connector.setAttribute(“keystorePass”,KEYSTORE\u SERVER\u PWD);
connector.setAttribute(“keystrefile”,
ksfPath.toFile().getAbsolutePath());
connector.setAttribute(“trustStorePassword”,TRUSTORE_服务器_PWD);
connector.setAttribute(“trustStoreFile”,
tsfPath.toFile().getAbsolutePath());
connector.setAttribute(“clientAuth”、“false”);
connector.setAttribute(“协议”、“HTTP/1.1”);
connector.setAttribute(“sslProtocol”、“TLSv1.2”);
connector.setAttribute(“maxThreads”、“200”);
connector.setAttribute(“SSLEnabled”,true);
返回连接器;
}    
私有静态ServletContainer resourceConfig(){
System.out.println(“扫描包:”+Main.class.getPackage().getName());
final ResourceConfig=new ResourceConfig()
.packages(Main.class.getPackage().getName())
.RegisterClass(GsonMessageBodyHandler.class
,SecurityFilter.class
,AuthenticationExceptionMapper.class)
.register(Resource.class)
.register(MyResource.class)
;
返回新的ServletContainer(配置);
}
}