2

我正在使用 jersey v2.21,我正在尝试设置我ObjectMapper的几个配置:

  1. 如果它的值是,不要返回一个属性null
  2. 返回按字母顺序排列的对象的属性
  3. 以如下格式返回日期:2015-06-22T17:57:05.000-07:00

这就是我的 ObjectMapper 的样子: 编辑:

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;

/**
 * Created by Shiran Maor on 1/13/16.
 */
@Provider
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {

    private ObjectMapper defaultMapper;

    public ObjectMapperResolver() {
        this.defaultMapper = createDefaultMapper();
    }

    @Override
    public ObjectMapper getContext(Class<?> aClass) {
        return this.defaultMapper;
    }

    private static ObjectMapper createDefaultMapper() {
        ObjectMapper result = new ObjectMapper();
        result = result.configure(SerializationFeature.INDENT_OUTPUT, true);
        result = result.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
        result = result.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

        result.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        result.setDateFormat(new ISO8601DateFormat());

        return result;

    }

}

编辑: 这些是我的依赖项:

    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.22.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.21</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-jaxb</artifactId>
      <version>2.21</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.7.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-jaxb-annotations</artifactId>
      <version>2.7.0</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.0</version>
  </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-json-provider</artifactId>
      <version>2.7.0</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-xml-provider</artifactId>
      <version>2.7.0</version>
      <scope>runtime</scope>
    </dependency>

编辑: 这是树依赖项:

[INFO] +- commons-codec:commons-codec:jar:1.6:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.1.1:compile
[INFO] +- org.apache.httpcomponents:httpcore:jar:4.1.1:compile
[INFO] +- commons-httpclient:commons-httpclient:jar:3.1:runtime
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.7:runtime
[INFO] +- org.slf4j:jul-to-slf4j:jar:1.7.7:runtime
[INFO] +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.7:runtime
[INFO] +- log4j:log4j:jar:1.2.14:compile
[INFO] +- org.mockito:mockito-all:jar:1.9.5:test
[INFO] +- org.jasig.cas.client:cas-client-core:jar:3.2.1:compile
[INFO] +- com.google.code.gson:gson:jar:1.7.1:compile
[INFO] +- org.testng:testng:jar:6.8.8:test
[INFO] |  +- org.beanshell:bsh:jar:2.0b4:test
[INFO] |  \- com.beust:jcommander:jar:1.27:test
[INFO] +- net.sf.ehcache:ehcache-core:jar:2.4.2:compile
[INFO] +- commons-cli:commons-cli:jar:1.2:runtime
[INFO] +- commons-io:commons-io:jar:1.4:compile
[INFO] +- commons-lang:commons-lang:jar:2.4:compile
[INFO] +- com.thoughtworks.xstream:xstream:jar:1.2.2:provided
[INFO] |  \- xpp3:xpp3_min:jar:1.1.4c:provided
[INFO] +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.22.1:runtime
[INFO] |  +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.22.1:runtime
[INFO] |  +- org.glassfish.jersey.core:jersey-common:jar:2.22.1:runtime
[INFO] |  |  \- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.22.1:runtime
[INFO] |  \- org.glassfish.jersey.core:jersey-server:jar:2.22.1:runtime
[INFO] |     \- org.glassfish.jersey.core:jersey-client:jar:2.22.1:runtime
[INFO] +- org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.21:runtime
[INFO] |  +- org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.21:runtime
[INFO] |  \- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.5.4:runtime
[INFO] +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.21:runtime
[INFO] |  +- org.glassfish.hk2:hk2-api:jar:2.4.0-b31:runtime
[INFO] |  |  +- org.glassfish.hk2:hk2-utils:jar:2.4.0-b31:runtime
[INFO] |  |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b31:runtime
[INFO] |  +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b31:runtime
[INFO] |  +- org.glassfish.hk2:hk2-locator:jar:2.4.0-b31:runtime
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:runtime
[INFO] |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:runtime
[INFO] +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] +- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.2:compile
[INFO] +- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.7.0:runtime
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.0:compile
[INFO] +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.7.0:runtime
[INFO] +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-xml-provider:jar:2.7.0:runtime
[INFO] |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.7.0:runtime
[INFO] |  +- org.codehaus.woodstox:stax2-api:jar:3.1.4:runtime
[INFO] |  \- org.codehaus.woodstox:woodstox-core-asl:jar:4.4.1:runtime
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- javax.servlet.jsp:javax.servlet.jsp-api:jar:2.3.1:provided
[INFO] +- org.apache.httpcomponents:httpmime:jar:4.1.1:test
[INFO] +- org.bouncycastle:bcprov-jdk15:jar:1.45:test
[INFO] +- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.2.11:compile
[INFO] +- com.sun.xml.bind:jaxb-impl:jar:2.2.11:runtime
[INFO] +- com.sun.xml.bind:jaxb-core:jar:2.2.11:runtime
[INFO] +- org.eclipse.persistence:eclipselink:jar:2.6.1:runtime
[INFO] |  +- org.eclipse.persistence:javax.persistence:jar:2.1.0:runtime
[INFO] |  +- org.eclipse.persistence:commonj.sdo:jar:2.1.1:runtime
[INFO] |  +- javax.validation:validation-api:jar:1.1.0.Final:runtime
[INFO] |  \- org.glassfish:javax.json:jar:1.0.4:runtime
[INFO] +- com.sun.xml.bind:jaxb-xjc:jar:2.2.11:runtime
[INFO] \- javax.xml.stream:stax-api:jar:1.0-2:runtime

编辑: 这是我的 web.xml 部分:

<servlet>
    <servlet-name>api-service-endpoints</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
                     com.mypack
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

但是,我得到null的不是字母,而是以毫秒为单位的日期。

更新: 更新依赖项后,我收到此异常:

        <pre>org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: com.fasterxml.jackson.jaxrs.cfg.EndpointConfigBase.&lt;init&gt;(Lcom/fasterxml/jackson/databind/cfg/MapperConfig;)V
    org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:278)
    org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:260)
    org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:509)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:334)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    com.anaplan.api.security.auth.AuthenticationFilter.doFilter(AuthenticationFilter.java:63)
</pre>

Update1: ​​我正在阅读有关球衣的依赖问题以及何时删除:

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <version>2.21</version>
  <scope>runtime</scope>
</dependency>

我得到了预期的结果,但是,只有在 xml 中,而不是在 json 中。有任何想法吗?

4

3 回答 3

1

添加依赖:

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-base</artifactId>
    <version>${jackson.version}</version>
</dependency>
于 2016-04-20T07:57:13.163 回答
1

添加此依赖项:

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <version>2.22.1</version>
  <scope>runtime</scope>
</dependency>

解决它。

于 2016-01-15T21:18:11.587 回答
0

ObjectMapper只要您正确使用它(例如,在 jax-rs 应用程序中注册 jackson 功能和注册您的提供程序),您就可以完美地工作(稍作修改)。

如果您在 JerseyMyObjectMapperProvider的示例中替换为对象映射器(请参阅https://github.com/jersey/jersey/tree/2.x/examples/json-jackson),请稍作修改json-jackson

private static ObjectMapper createDefaultMapper() {

    ObjectMapper result = new ObjectMapper();
    result = result.configure(SerializationFeature.INDENT_OUTPUT, true);
    result = result.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    result = result.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

    result.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    result.setDateFormat(new ISO8601DateFormat());

    return result;
}

它会工作得很好。

于 2016-01-15T08:56:52.007 回答