本文介绍了ContainerRequestFilter不会在JavaEE jersey项目中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

JavaEE的新手,让自定义ContainerRequestFilter在Jersey中运行时会遇到一些问题.我阅读了更多的球衣文档,并直接从' jersey-quickstart-webapp '创建了一个新的干净项目,添加了下面看到的过滤器,但没有运气(添加了一个空白以及bean.xml).

Am new to JavaEE and have some issues getting custom ContainerRequestFilter to run in Jersey. I read the jersey documentation some more and created a new clean project straight from the 'jersey-quickstart-webapp', added the filter seen below but no luck (added an empty beans.xml as well).

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

@Provider
@PreMatching
public class MyFilter implements ContainerRequestFilter {
    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
    }
}

不确定Prematching和Provider是否互补,因此我分别使用了两者,但是没有用(MyReasource只是用作没有过滤器).尝试在MyFilter中引发异常,但未运行.

Was uncertain if Prematching and Provider was complementary or not so i used both then each separately, but didnt work (MyReasource just served as without filter). Tried throwing an exception in MyFilter but that didnt run either.

所以我搜索了StackOverflow,发现' http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/"指出您实际上需要在Application中实现注册或ResourceConfig类.我尝试了此操作(尽力而为),但是现在我至少收到了有关资源类的警告," 没有找到资源类a.b.MyFilter 的资源方法"

So i searched through StackOverflow and found 'http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/' which points to that you actually needs to implement registrations in Application or ResourceConfig class. I tried this (didnt work) but i atleast got a warning for the resource class now, 'No resource methods have been found for resource class a.b.MyFilter'

我的Application类现在如下所示(尝试扫描程序包,但没有产生任何变化.没有手动进行过滤器注册,我也没有得到警告).

My Application class now looks like below (tried scan package but didnt make a difference. Without the manual filter registration i didnt get the warning either).

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import javax.ws.rs.ApplicationPath;

@ApplicationPath("resources")
public class RestApplication extends ResourceConfig {
    public RestApplication() {
        //packages("a.b");
        register(MyFilter.class);
        register(MyResource.class);
        property(ServerProperties.TRACING, "ALL");
    }
}

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">
</web-app>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>a.b</groupId>
    <artifactId>server</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>server</name>
    <build>
        <finalName>server</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.22.1</jersey.version>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

所有文件都位于"a.b"(即我的包)根目录中.关于如何使过滤器实际运行的任何想法,我都将非常感激;).我想让它工作起来应该不那么困难,所以我想我在这里错过了一些东西吗?

All files lies in the 'a.b' (i.e my package) root. Any ideas on how to get the filter actually running and i would be very greatful ;). I presume it shouldnt be this hard to get this working so i guess im missing something here?

推荐答案

让我向您介绍发生的情况.当您第一次创建jersey-quickstart-webapp原型时,它给了您

Let me walk you through what's going on. When you first created the jersey-quickstart-webapp archetype, it gave you this

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>a.b</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

此init-param jersey.config.server.provider.packages的作用是告诉Jersey使用哪些软件包扫描用@Path注释的资源类以及用@Provider注释的提供程序类.

What this init-param jersey.config.server.provider.packages does is tell Jersey what package(s) to scan for resource classes annotated with @Path, and provider classes annotated with @Provider.

因此,从这一点开始,您要做的就是将@Provider添加到过滤器中,这样就可以了.

So from that point, all you needed to do was add the @Provider to the filter, and it would have worked.

但是随后您决定清除web.xml,并将ResourceConfig@ApplicationPath一起使用.为此,Jersey充分利用了Servlet 3.0的可插入性机制,如在此答案中所述.为此,我们需要确保我们有带有JerseyServletContainerInitializer的罐子.那是我们需要查看pom.xml文件的地方

But then you decided to clear out the web.xml and use the ResourceConfig with the @ApplicationPath. For this to work Jersey takes advantage of the Servlet 3.0 pluggability mechanism, as mentioned in this answer. For that to work we need to make sure we have the jar that has the JerseyServletContainerInitializer. That's where we need to look at the pom.xml file

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
    <!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>

此评论告诉您,如果不需要Servlet 2.5支持,则应使用jersey-container-servlet而不是jersey-container-servlet-core.我不知道,它的措词可能不正确.也许相反,它应该说如果您想要Servlet 3.x支持,请对其进行更改.但是无论如何,jersey-container-servlet具有我们需要的JerseyContainerServletInitializer.因此,如果您要减少使用web.xml的内容,则只需切换依赖项即可.

The comment is telling you that if you don't need Servlet 2.5 support, you should use jersey-container-servlet instead of jersey-container-servlet-core. I don't know, it might be poorly worded. Maybe instead it should say if you want Servlet 3.x support, change it. But in any case, the jersey-container-servlet has the JerseyContainerServletInitializer that we need. So if you want to go web.xml-less, then just switch out the dependency.

这篇关于ContainerRequestFilter不会在JavaEE jersey项目中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:41