Jersey 2 + Jackson Annotation / @JsonIgnore

2020-03-30 04:10发布

EDIT: Being more specific now i noticed a conflict i want to use BOTH dependencies below:

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-binding</artifactId>
        <version>2.27</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.3.1</version>
    </dependency>

Basically, I am trying to ignore a property (@JsonIgnore), but none of my Jackson annotations are working. Even the @JsonProperty. I tried to add the @JsonIgnore in getters and setters methods, but same behavior.

I also tried to follow official documentation, and tried different libraries

  • import org.codehaus.jackson.annotate.JsonIgnore; (Same Behavior)
  • import com.fasterxml.jackson.annotation.JsonIgnore; (Same Behavior)

I see similar posts like #12595351

My Response from the Controller, should not display the Revoked. Attribute, but i got this response:

Actual Response

{
    "accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjUzMzI5OTV9.uri3pRwXQHHG09F-wM40qfuRMRVu_WBK3HlfquGvwYc",
    "expiresAt": "2018-05-03T07:36:35.087Z[UTC]",
    "expiresIn": 7199,
    "issuedAt": "2018-05-03T05:36:35.087Z[UTC]",
    "refreshToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjU5MzA1OTV9.xj2oytAVwiAIR8U2upJkPH_BdORuJUNbiicvuvGFz0w",
    "revoked": false,
    "type": "Bearer"
}

Expected Response

{
    "accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjUzMzI5OTV9.uri3pRwXQHHG09F-wM40qfuRMRVu_WBK3HlfquGvwYc",
    "expiresAt": "2018-05-03T07:36:35.087Z[UTC]",
    "expiresIn": 7199,
    "issuedAt": "2018-05-03T05:36:35.087Z[UTC]",
    "refreshToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjU5MzA1OTV9.xj2oytAVwiAIR8U2upJkPH_BdORuJUNbiicvuvGFz0w",
    "type": "Bearer"
}

pom.xml (Using Maven)

<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>com.wedhany.fimper</groupId>
<artifactId>fimper</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>fimper</name>

<build>
    <finalName>fimper</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<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>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>de.mkammerer</groupId>
        <artifactId>argon2-jvm</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-dbcp</artifactId>
        <version>9.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.0.7</version>
    </dependency>
    <dependency>
        <groupId>org.modelmapper</groupId>
        <artifactId>modelmapper</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-binding</artifactId>
        <version>2.27</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring4</artifactId>
        <version>2.27</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-jdk-http</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.17.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.9.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>
</dependencies>

<profiles>
    <profile>
        <id>Development</id>
        <dependencies>
            <dependency>
                <groupId>com.github.blocoio</groupId>
                <artifactId>faker</artifactId>
                <version>1.2.7</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>

<properties>
    <jersey.version>2.27</jersey.version>
    <springframework.version>4.3.16.RELEASE</springframework.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Token.java (My Model)

package com.wedhany.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.wedhany.models.enums.token.GrantType;
import com.wedhany.models.enums.token.Type;

import java.util.Date;

public class Token {

/**
 * Attributes
 */
private String accessToken;
private String refreshToken;

@JsonIgnore
private boolean revoked;

@JsonProperty("expires_at")
private Date expiresAt;
private Date issuedAt;

private GrantType grantType;
private Type type;

private User user;

/**
 * @return Token TTL in seconds.
 */
public long getExpiresIn() {
    return this.expiresAt.getTime() < new Date().getTime()
            ? 0
            : (this.expiresAt.getTime() - new Date().getTime()) / 1000;
}

/**
 * @return Token that will grant authentication and authorization.
 */
public String getAccessToken() {
    return accessToken;
}

/**
 * @param accessToken Token string.
 */
public void setAccessToken(String accessToken) {
    this.accessToken = accessToken;
}

/**
 * @return Token used to request a new token.
 */
public String getRefreshToken() {
    return refreshToken;
}

/**
 * @return Invalid token if true.
 */
public boolean isRevoked() {
    return revoked;
}

/**
 * @param revoked True for invalid.
 */
public void setRevoked(boolean revoked) {
    this.revoked = revoked;
}

/**
 * @param refreshToken Refresh token.
 */
public void setRefreshToken(String refreshToken) {
    this.refreshToken = refreshToken;
}

/**
 * @return Token's expiration date.
 */
public Date getExpiresAt() {
    return expiresAt;
}

/**
 * @param expiresAt Token's expiration date.
 */
public void setExpiresAt(Date expiresAt) {
    this.expiresAt = expiresAt;
}

/**
 * @return Date where the token was requested.
 */
public Date getIssuedAt() {
    return issuedAt;
}

/**
 * @param issuedAt Date where the token was requested.
 */
public void setIssuedAt(Date issuedAt) {
    this.issuedAt = issuedAt;
}

/**
 * @return Type of the token.
 */
public Type getType() {
    return type;
}

/**
 * @param type Type of the token.
 */
public void setType(Type type) {
    this.type = type;
}

/**
 * @return How the token was claimed.
 */
public GrantType getGrantType() {
    return grantType;
}

/**
 * @param grantType Set token type of grant.
 */
public void setGrantType(GrantType grantType) {
    this.grantType = grantType;
}

/**
 * @return Owner of the token
 */
public User getUser() {
    return user;
}

/**
 * @param user Token's owner.
 */
public void setUser(User user) {
        this.user = user;
    }
}

AuthenticationController

package com.wedhany.controllers;

import com.wedhany.exceptions.AuthorizationException;
import com.wedhany.models.Token;
import com.wedhany.models.User;
import com.wedhany.services.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.security.sasl.AuthenticationException;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("auth")
public class AuthenticationController {

@Autowired
private AuthenticationService authenticationService;

@POST
@Path("login")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response login(User user, @HeaderParam("user-agent") String userAgent) throws Exception {
    try {
        // Authenticate the user using the credentials provided
        this.authenticationService.authenticate(user.getEmail(), user.getPassword());

        // Issue a token for the user
        Token token = this.authenticationService.issueToken(user.getEmail(), userAgent);

        // Return the token on the response
        return Response.ok(token).build();

    } catch (AuthorizationException e) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    } catch (AuthenticationException e) {
        return Response.status(Response.Status.FORBIDDEN).build();
    }
}

@POST
@Path("refresh")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response refresh(Token token, @HeaderParam("user-agent") String userAgent) throws AuthenticationException {
    return Response.status(Response.Status.CREATED)
            .entity(this.authenticationService.refresh(token.getRefreshToken(), userAgent))
            .build();
}

@POST
@Path("register")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response register(User user) {
    user = authenticationService.save(user);

    return Response.status(Response.Status.CREATED)
            .entity(user)
            .build();
}
}

5条回答
干净又极端
2楼-- · 2020-03-30 04:37

I don't know the whole configuration of your project so one think you can do that is, create manually JSON and then send to response like:

ObjectMapper maper = new ObjectMapper();
return Response.ok(maper.writer().withDefaultPrettyPrinter().writeValueAsString(tokenObject));

It will work like manual conversion without using auto serialization by Jersey.
Note: This thing is not recommendable but it should work.

You need this dependency for conversion:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
</dependency>
查看更多
该账号已被封号
3楼-- · 2020-03-30 04:42

Basically the jersey-media-json-binding and jersey-media-json-jackson have similar behavior. You can't use both at the same time. The reason the jersey-media-json-jackson was not working it is because the provider which have more priority is the jersey-media-json-binding.

查看更多
地球回转人心会变
4楼-- · 2020-03-30 04:42

This works for me, I have these libs in my pom.xml:

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${org.glassfish.jersey.core.version}</version>
            <scope>provided</scope>
        </dependency>


        <!-- ************** Jackson XML and JSON API ************************* -->          
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-version}</version>
        </dependency>   

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

Just remove that property from your class and add this annotation:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class Token {
// ... keep only the properties you want to map

this will tell Jackson to only bind the properties which you actually have in your class ignoring all the rest that might be present in the JSON output.

查看更多
霸刀☆藐视天下
5楼-- · 2020-03-30 04:47

Choose either one of the following but not both:

<!-- JSON-B (JSR-347) support -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-binding</artifactId>
    <version>2.27</version>
</dependency>
<!-- Jackson 2.x support -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.27</version>
</dependency>

Both Jackson and JSON-B provide JSON from/to Java binding:

  • Jackson is a quite mature library for JSON processing. It's flexible and has a fair number of extensions modules.

  • JSON-B is also referenced as JSR-347. It's an specification for JSON binding. The actual implementation will be provided by Eclipse Yasson, which is the reference implementation of the JSR-347.


If you want go for jersey-media-json-jackson, you are supposed to use Jackson annotations. To ignore a property, for instance, use @JsonIgnore.

If you want to go for jersey-media-json-binding, you are supposed to use JSON-B annotations. To ignore a property, for instance, use @JsonbTransient.


You are using jersey-bom, a dependency management artifact that consolidate and centralize the management of dependency versions (without actually adding the dependencies to the project).

So you don't need to specify the version of the org.glassfish.jersey artifacts. Use one of the following (without version):

<!-- JSON-B (JSR-347) support -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-binding</artifactId>
</dependency>
<!-- Jackson 2.x support -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
</dependency>

See more details here and here.

查看更多
ゆ 、 Hurt°
6楼-- · 2020-03-30 04:47

The following code works for me with jackson version 2.8.10

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonIgnoreExample {
    private static class BeanWithIgnore {
        @JsonIgnore
        public int id;
        public String name;

        public BeanWithIgnore(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");
        String result = new ObjectMapper().writeValueAsString(bean);
        System.out.println(result); // {"name":"My bean"}
    }
}
查看更多
登录 后发表回答