JAX RS(Jersey 1.19)无法调用Filter

时间:2017-08-08 08:36:44

标签: java rest filter jersey jax-rs

以下是我的过滤器

    package com.rest.echidna.filter;

import javax.ws.rs.ext.Provider;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

@Provider
public class CustomRestFilter implements ContainerRequestFilter {

    @Override
    public ContainerRequest filter(ContainerRequest arg0) {
        System.out.println("CustomRestFilter.filter()");
        return null;
    }

}

控制器 - >

package com.rest.echidna.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import com.rest.echidna.model.User;
import com.rest.echidna.model.UserList;
import com.sun.jersey.api.client.ClientResponse.Status;

@Path(value = "/user/")
public class UserRestController {

    private static final List<User> usersDB = new ArrayList<User>();

    static {
        System.out.println("Inside JERSEY");
        usersDB.add(new User("A", 26, "SSE"));
        usersDB.add(new User("B", 28, "SSE"));
        usersDB.add(new User("C", 29, "TA"));
        usersDB.add(new User("D", 31, "TA"));
        usersDB.add(new User("E", 35, "TL"));

    }

    @Path(value = "/getAllUser")
    @GET
    public Response getAllUsers(@HeaderParam(value = "Accept") String ht ) {
System.out.println(ht);

        return Response.status(Status.OK).header("A", 123).entity(new UserList(usersDB)).build();

    }

    @Path(value = "/getAllUser/{name}")
    @GET
    public User getUser(@PathParam("name") String name,@Context HttpHeaders ht) {
        for(Entry<String, List<String>> m:ht.getRequestHeaders().entrySet()){
            System.out.println(m.getValue());
        }
        User resultUser = null;
        for (User u : usersDB) {
            if (u.getName().equalsIgnoreCase(name)) {
                resultUser = u;
            }
        }

        return resultUser;
    }

    @Path(value = "/getAllUser/{name}")
    @DELETE
    public UserList deleteUser(@PathParam("name") String name) {

        Iterator<User> it = usersDB.iterator();
        while (it.hasNext()) {
            if (it.next().getName().equalsIgnoreCase(name)) {
                it.remove();
            }
        }

        return new UserList(usersDB);
    }

    @Path(value = "/getAllUser/create")
    @POST
    public UserList createUser(User user) {

        System.out.println(user);
        usersDB.add(user);

        return new UserList(usersDB);
    }

    @Path(value = "/getAllUser/{name}")
    @PUT
    public UserList createUser(@PathParam("name") String name, User user) {

        User tempUser = null;
        Iterator<User> it = usersDB.iterator();

        while (it.hasNext()) {
            tempUser = it.next();
            if (tempUser.getName().equalsIgnoreCase(name)) {
                tempUser.setAge(user.getAge());
                tempUser.setRole(user.getRole());

                it.remove();
                break;
            }
        }
        usersDB.add(tempUser);
        return new UserList(usersDB);
    }

}

的web.xml

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

  </welcome-file-list>

  <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
        <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>com.rest.echidna</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>com.rest.echidna.filter.CustomRestFilter</param-value>
        </init-param>

    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

罐 - &GT; JARS

我评论了Filter params,即使我删除了注释也无效。 点击网址后没有调用过滤器,但是主控制器正在调用。我用google搜索但是没能找到适合泽西1.19过滤器的解决方案。请帮助!!!

在本地服务器中点击的网址 - &gt; http://localhost:8080/RESTJersey/rest/user/getAllUser

1 个答案:

答案 0 :(得分:0)

您的初始化参数名称不正确

请从

更改
com.sun.jersey.spi.container.ContainerRequestFilter

com.sun.jersey.spi.container.ContainerRequestFilters

更新

以下是对上述项目所做的更改

  • 添加依赖项jersey-grizzly2

根据Jersey 1,您需要为项目提供两个依赖项

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-server</artifactId>
  <version>1.19.1</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-grizzly2</artifactId>
  <version>1.19.1</version>
</dependency>
  • 您需要在CustomRestFilter中返回相同的请求对象。我已将您的问题更新了。

  • 请更正您希望如何返回UserList对象的代码。如果您想将其作为json返回,请查看here

我可以在控制台上看到它:

09-Aug-2017 19:24:02.503 INFO [http-nio-8080-exec-32] com.sun.jersey.server.impl.application.WebApplicationImpl._initiate Initiating Jersey application, version 'Jersey: 1.19.1 03/11/2016 02:08 PM'
CustomRestFilter.filter()
Inside JERSEY
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8