如何使用org.glassfish.jersey实现Jersey ContainerRequest示例

时间:2014-08-26 16:21:39

标签: java jersey

我遇到了一些编码问题,并且正在尝试实现此代码...但需要使用org.glassfish.jersey而且类结构看起来非常不同。

https://github.com/tinkerpop/rexster/blob/master/rexster-server/src/main/java/com/tinkerpop/rexster/filter/HeaderResponseFilter.java

任何建议都表示赞赏,但我甚至无法在依赖的jersey包结构中找到ContainerResponseFilter,其他依赖关系似乎在

import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ContainerResponse;

不是示例中的spi包。

我的pom看起来像这样:

<!-- JAX-RS Dependencies -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>

        <!-- JAX-RS application servlet -->
        <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-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-bean-validation</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- Jersey Spring bridge to allow spring resources to be used from REST resources -->
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
            <version>${jersey.version}</version>
        </dependency>

由于

1 个答案:

答案 0 :(得分:0)

我是这样做的:

package com.mycompany.misf.filters;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MediaType;


public class HeaderResponseFilter implements ContainerResponseFilter {

        public void filter(ContainerRequestContext request, ContainerResponseContext response) {
            MediaType type = response.getMediaType();
            if (type != null) {
                String contentType = type.toString();
                if (!contentType.contains("charset")) {
                    contentType = contentType + ";charset=utf-8";
                    response.getHeaders().putSingle("Content-Type", contentType);
                }
            }
        }
}