2015-12-13 28 views
1

我對Wildfly和Java EE的某些部分相當陌生。Wildfly和Jackson @JsonIgnore註釋

我有一個休息服務使用RestEasy在wildfly上運行。我的'用戶'實體有一個'AccessToken'實體。理想情況下,我希望能夠將用戶實體作爲JSON發送,而不必發送訪問令牌。

我做了一些研究,發現我應該可以使用@JsonIgnore來完成這個。然而,這個註釋是不可用的 - 可能是我的POM中的一個錯誤。

如果我理解正確,Wildfly會使用Jackson,因此註釋應該「提供」。我用了一個「bom」,我認爲是所有提供的部分,但我錯過了什麼?

這裏是我的pom.xml它源於快速入門通過的IntelliJ:

<?xml version="1.0" encoding="UTF-8"?> 
JBoss, Home of Professional Open Source 
Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual 
contributors by the @authors tag. See the copyright.txt in the 
distribution for a full listing of individual contributors. 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 
http://www.apache.org/licenses/LICENSE-2.0 
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 

<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.example</groupId> 
<artifactId>myproject</artifactId> 
<version>1.0-SNAPSHOT</version> 
<packaging>war</packaging> 
<name>WildFly Quickstarts: example</name> 
<description>A starter Java EE 7 webapp project for use on JBoss WildFly/WildFly, generated from the jboss-javaee6-webapp archetype</description> 


<properties> 
    <!-- Explicitly declaring the source encoding eliminates the following 
     message: --> 
    <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered 
     resources, i.e. build is platform dependent! --> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 

    <!-- JBoss dependency versions --> 
    <version.wildfly.maven.plugin>1.0.2.Final</version.wildfly.maven.plugin> 

    <!-- Define the version of the JBoss BOMs we want to import to specify 
     tested stacks. --> 
    <version.jboss.bom>8.0.0.Final</version.jboss.bom> 

    <!-- other plugin versions --> 
    <version.compiler.plugin>3.1</version.compiler.plugin> 
    <version.surefire.plugin>2.16</version.surefire.plugin> 
    <version.war.plugin>2.5</version.war.plugin> 

    <!-- maven-compiler-plugin --> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    <maven.compiler.source>1.8</maven.compiler.source> 
</properties> 


<dependencyManagement> 
    <dependencies> 
     <!-- JBoss distributes a complete set of Java EE 7 APIs including a Bill 
      of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection) 
      of artifacts. We use this here so that we always get the correct versions 
      of artifacts. Here we use the jboss-javaee-7.0-with-tools stack (you can 
      read this as the JBoss stack of the Java EE 7 APIs, with some extras tools 
      for your project, such as Arquillian for testing) and the jboss-javaee-7.0-with-hibernate 
      stack you can read this as the JBoss stack of the Java EE 7 APIs, with extras 
      from the Hibernate family of projects) --> 
     <dependency> 
      <groupId>org.wildfly.bom</groupId> 
      <artifactId>jboss-javaee-7.0-with-tools</artifactId> 
      <version>${version.jboss.bom}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.wildfly.bom</groupId> 
      <artifactId>jboss-javaee-7.0-with-hibernate</artifactId> 
      <version>${version.jboss.bom}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 

    <!-- First declare the APIs we depend on and need for compilation. All 
     of them are provided by JBoss WildFly --> 

    <!-- Import the CDI API, we use provided scope as the API is included in 
     JBoss WildFly --> 
    <dependency> 
     <groupId>javax.enterprise</groupId> 
     <artifactId>cdi-api</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- Import the Common Annotations API (JSR-250), we use provided scope 
     as the API is included in JBoss WildFly --> 
    <dependency> 
     <groupId>org.jboss.spec.javax.annotation</groupId> 
     <artifactId>jboss-annotations-api_1.2_spec</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- Import the JAX-RS API, we use provided scope as the API is included 
     in JBoss WildFly --> 
    <dependency> 
     <groupId>org.jboss.resteasy</groupId> 
     <artifactId>jaxrs-api</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- Import the JPA API, we use provided scope as the API is included in 
     JBoss WildFly --> 
    <dependency> 
     <groupId>org.hibernate.javax.persistence</groupId> 
     <artifactId>hibernate-jpa-2.1-api</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- Import the EJB API, we use provided scope as the API is included in 
     JBoss WildFly --> 
    <dependency> 
     <groupId>org.jboss.spec.javax.ejb</groupId> 
     <artifactId>jboss-ejb-api_3.2_spec</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>com.google.code.gson</groupId> 
     <artifactId>gson</artifactId> 
     <version>2.5</version> 
    </dependency> 


    <!-- JSR-303 (Bean Validation) Implementation --> 
    <!-- Provides portable constraints such as @Email --> 
    <!-- Hibernate Validator is shipped in JBoss WildFly --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
     <scope>provided</scope> 
     <exclusions> 
      <exclusion> 
       <groupId>org.slf4j</groupId> 
       <artifactId>slf4j-api</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

    <!-- Import the JSF API, we use provided scope as the API is included in 
     JBoss WildFly --> 
    <dependency> 
     <groupId>org.jboss.spec.javax.faces</groupId> 
     <artifactId>jboss-jsf-api_2.2_spec</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- Now we declare any tools needed --> 

    <!-- Annotation processor to generate the JPA 2.0 metamodel classes for 
     typesafe criteria queries --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-jpamodelgen</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- Annotation processor that raising compilation errors whenever constraint 
     annotations are incorrectly used. --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator-annotation-processor</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- Needed for running tests (you may also use TestNG) --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <!-- Optional, but highly recommended --> 
    <!-- Arquillian allows you to test enterprise code such as EJBs and Transactional(JTA) 
     JPA from JUnit/TestNG --> 
    <dependency> 
     <groupId>org.jboss.arquillian.junit</groupId> 
     <artifactId>arquillian-junit-container</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.jboss.arquillian.protocol</groupId> 
     <artifactId>arquillian-protocol-servlet</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <!-- Facebook library --> 
    <dependency> 
     <groupId>com.restfb</groupId> 
     <artifactId>restfb</artifactId> 
     <version>1.17.0</version> 
    </dependency> 


</dependencies> 

<build> 
    <!-- Maven will append the version to the finalName (which is the name 
     given to the generated war, and hence the context root) --> 
    <finalName>${project.artifactId}</finalName> 
    <plugins> 
     <plugin> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>${version.war.plugin}</version> 
      <configuration> 
       <!-- Java EE 7 doesn't require web.xml, Maven needs to catch up! --> 
       <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration> 
     </plugin> 
     <!-- The WildFly plugin deploys your war to a local WildFly container --> 
     <!-- To use, run: mvn package wildfly:deploy --> 

    </plugins> 
</build> 

<profiles> 
    <profile> 
     <!-- The default profile skips all tests, though you can tune it to run 
      just unit tests based on a custom pattern --> 
     <!-- Seperate profiles are provided for running all tests, including Arquillian 
      tests that execute in the specified container --> 
     <id>default</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <build> 
      <plugins> 
       <plugin> 
        <artifactId>maven-surefire-plugin</artifactId> 
        <version>${version.surefire.plugin}</version> 
        <configuration> 
         <skip>true</skip> 
        </configuration> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 

</profiles> 

我也嘗試添加:

<dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-annotations</artifactId> 
     <version>2.6.4</version> 
    </dependency> 

這讓我使用@JsonIgnore但隨後仍然在返回給客戶端的JSON中顯示這個字段。我想也許我的wild is正在使用老版本的jackson(1.x)而不是2? (這給我的印象:JsonIgnoreProperties not working

回答

2

註釋@JsonIgnore是傑克遜註釋jar的一部分。 要包含它,請在您的pom文件中使用以下依賴項。

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-annotations</artifactId> 
    <version>2.6.4</version> 
</dependency> 
+0

謝謝 - 但添加後,雖然我可以在我的實體字段上使用@JsonIgnore,它仍然顯示時返回它。爲了確保它不會干涉,我也從我的pom.xml中刪除了Gson並使用它。 – ThePerson

+0

啊,...所以在升級到Wildfly 9之後,這種方法可行。我猜想,因爲它使用的是新版本。現在我想我需要更新我使用的「bom」,它會很好地運行。謝謝。 如果你可以添加到你的答案,這與蜻蜓9一起工作,但蜻蜓8似乎使用舊的傑克遜版本,這不適用於它可能會幫助其他人。 謝謝! – ThePerson

相關問題