gpt4 book ai didi

java - 迁移 Jersey 2.22.1 至 Jersey 2.22.2

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:54 26 4
gpt4 key购买 nike

无法从 Jersey 2.22.1 版本升级到 2.22.2 版本。
层次结构:
enter image description here
build.gradle 中的依赖项部分:

def jerseyVersion = '2.22.1'
def hk2Version = '2.4.0-b31'
def giuceVersion = '4.0'

dependencies {
//javax
compile "javax.servlet:javax.servlet-api:3.1.0"
//jersey
compile "org.glassfish.jersey.core:jersey-server:${jerseyVersion}"
compile "org.glassfish.jersey.containers:jersey-container-servlet:${jerseyVersion}"
//hk2
compile "org.glassfish.hk2:guice-bridge:${hk2Version}"
//guice
compile "com.google.inject:guice:${giuceVersion}"
compile "com.google.inject.extensions:guice-servlet:${giuceVersion}"
}

文件 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>REST API App</display-name>

<listener>
<listener-class>com.example.core.JerseyGuiceServletContextListener</listener-class>
</listener>

<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

JerseyGuiceServletContextListener 类:

package com.example.core;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;

public class JerseyGuiceServletContextListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModuleConfig());
}
}

JerseyServletModuleConfig 类:

package com.example.core;

import com.google.inject.Scopes;
import com.google.inject.servlet.ServletModule;
import com.example.ws.HeyResource;
import org.glassfish.jersey.servlet.ServletContainer;

import java.util.Map;
import java.util.TreeMap;

class JerseyServletModuleConfig extends ServletModule {
@Override
protected void configureServlets() {
Map<String, String> servletContainerParams = new TreeMap<>();
servletContainerParams.put("javax.ws.rs.Application", JerseyConfiguration.class.getCanonicalName());
bind(ServletContainer.class).in(Scopes.SINGLETON);
filter("/*").through(ServletContainer.class, servletContainerParams);
bind(HeyResource.class).in(Scopes.SINGLETON);
}
}

JerseyConfiguration 类:

package com.example.core;

import com.google.inject.Injector;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.server.ResourceConfig;
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;

import javax.inject.Inject;
import javax.servlet.ServletContext;

class JerseyConfiguration extends ResourceConfig {
@Inject
public JerseyConfiguration(ServiceLocator serviceLocator, ServletContext servletContext) {
packages("com.example.ws");
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector((Injector) servletContext.getAttribute(Injector.class.getName()));
}
}

在 2.22.1 Jersey 版本中,一切都运行良好。当我更改 2.22.2 上的版本时 - 我遇到了异常:

21-Aug-2016 12:34:12.577 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.filterStart Exception starting filter guiceFilter java.lang.NullPointerException at org.glassfish.jersey.servlet.init.FilterUrlMappingsProviderImpl.getFilterUrlMappings(FilterUrlMappingsProviderImpl.java:66) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:444)

我认为 guice-bridge 版本可以解决这个问题。我已将版本 2.4.0-b31 替换为版本 2.4.0-b34(用于 org.glassfish.jersey.core:jersey-server 中的各种 hk2 依赖项) - 没有帮助。

链接到 FilterUrlMappingsProviderImpl 类:https://github.com/jersey/jersey/blob/79d7767be5102f57520cf23900cad3a7ef4230ad/containers/jersey-servlet/src/main/java/org/glassfish/jersey/servlet/init/FilterUrlMappingsProviderImpl.java

从上面描述的 FilterUrlMappingsProviderImpl 类的链接可以清楚地看出,不可能获取 FilterRegistration 类对象。

可能存在什么问题?您对她的决定有何建议?谢谢。

最佳答案

解决方案

<小时/>

文件 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>REST API App</display-name>

<listener>
<listener-class>com.example.core.JerseyGuiceServletContextListener</listener-class>
</listener>

<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>jerseyFilter</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.example.core.JerseyConfiguration</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jerseyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

JerseyServletModuleConfig 类:

package com.example.core;

import com.google.inject.Scopes;
import com.google.inject.servlet.ServletModule;
import com.example.ws.HeyResource;
import org.glassfish.jersey.servlet.ServletContainer;

import java.util.Map;
import java.util.TreeMap;

class JerseyServletModuleConfig extends ServletModule {
@Override
protected void configureServlets() {
bind(HeyResource.class).in(Scopes.SINGLETON);
}
}

关于java - 迁移 Jersey 2.22.1 至 Jersey 2.22.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com