最新杰克逊使用球衣2.22.1获得Json成绩

时间:2016-01-15 00:14:30

标签: json jersey jackson jersey-2.0 objectmapper

我正在使用jersey v2.21,我试图将ObjectMapper设置为几个配置:

  1. 如果属性值为null
  2. ,则不返回该属性
  3. 按字母顺序返回对象的属性
  4. 以下列格式返回日期:2015-06-22T17:57:05.000-07:00
  5. 这是我的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中。 任何想法?

3 个答案:

答案 0 :(得分:1)

添加此依赖项:

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

修正了它。

答案 1 :(得分:1)

添加依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-base</artifactId>
    <version>${jackson.version}</version>
</dependency>

答案 2 :(得分:0)

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

如果您将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; } 示例中的对象映射器(请参阅https://github.com/jersey/jersey/tree/2.x/examples/json-jackson)略微修改

$.ajax({
    type: "get",  // or post method, your choice
    url: yourFileForInclude.php, // any url in same origin
    data: data,  // data if you need send some data to page
    success: function(msg){
                console.log(msg); // for checking
                window.open('about:blank').document.body.innerHTML = msg;  // you can change  about:blank  to an url.
               }
}); 

它会正常工作。