@JsonView不适用于REST服务

时间:2018-06-12 01:25:05

标签: json hibernate rest maven java-ee

我使用hibernate创建了一个带有 Client 实体的简单maven项目,以及它的DAO实现,我还有一个服务类 ClientResource ,带有一些 @POST < / em>和 @GET 方法,我想使用 @JsonView 来获取名称 id 客户端,但注释对我不起作用,当我使用Postman测试我的服务时,它给了我所有的属性!

客户实体

package Entities;

import Services.Views;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

@Entity
@Table(name = "Clienttable",
        uniqueConstraints = {
                @UniqueConstraint(name = "nom_prenom", columnNames = {"nom", "prenom"})
        })
public class Client implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonView(Views.Public.class)
    private int idClient;

    @Column(nullable = false, length = 20)
    @JsonView(Views.Public.class)
    private String nom;

    @Column(nullable = false, length = 20)
    private String prenom;

    @Column(nullable = false, length = 5)
    @Enumerated(EnumType.STRING)
    private Civility sexe;

 public int getIdClient() {
    return idClient;
}

public void setIdClient(int idClient) {
    this.idClient = idClient;
}

public String getNom() {
    return nom;
}

public void setNom(String nom) {
  this.nom = nom;
 }

public Civility getSexe() {
    return sexe;
}

public void setSexe(Civility sexe) {
    this.sexe = sexe;
}

public String getPrenom() {
    return prenom;
}

public void setPrenom(String prenom) {
    this.prenom = prenom;
}
public Client() {
}

ClientRessource

package Services;


import DAO.IClientDAOLocal;
import Entities.Client;
import com.fasterxml.jackson.annotation.JsonView;

import javax.ejb.EJB;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;

@Path("/client")

public class ClientRessource {
@EJB
IClientDAOLocal iClientDAOLocal;

@GET
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public Client find(@QueryParam("id") int id)
{

    return iClientDAOLocal.find(id);
}

@GET
@Path("/{id}")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@JsonView(Views.Public.class)
public Client find2(@PathParam("id") int id)
{

    return iClientDAOLocal.find(id);
}

@GET
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Path("/all")
public ArrayList<Client> getAllClients()
{

    return iClientDAOLocal.findAll();
}

@GET
@Produces({MediaType.TEXT_PLAIN})
@Path("/count")
public Long count()
{

    return iClientDAOLocal.count();
}

@POST
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public void add(Client c)
{
    iClientDAOLocal.create(c);
}

@PUT
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public void update(Client c)
{
    iClientDAOLocal.edit(c);
}

@DELETE
@Path("/delete/{id}")
public void delete( @PathParam("id") int id)
{
    iClientDAOLocal.remove(iClientDAOLocal.find(id));
}

}

浏览

    package Services;

public class Views {

    public static class Public { }

    public static class Internal extends Public { }


}

的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>tpMaven</groupId>
    <artifactId>tpMaven</artifactId>
    <version>1.0-SNAPSHOT</version>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <properties>
        <jersey.version>2.26</jersey.version>
        <jaxrs.version>2.0</jaxrs.version>
    </properties>
    <dependencies>


        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>


        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.12.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.primefaces/primefaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>6.1</version>
        </dependency>


    </dependencies>

</project>

1 个答案:

答案 0 :(得分:0)

package Services;

public class Views {

    public interface Public { }

    public interface Internal extends Public { }


}

观看中进行以下更改可能有所帮助。

由于