简体   繁体   English

@JsonView不适用于REST服务

[英]@JsonView doesn't work with REST service

I create a simple maven project with a Client entity using hibernate, and its DAO implementation, I have also a service class ClientResource with some @POST and @GET methods, I want to use @JsonView to get just the name and the id of the Client but the annotation doesn't work for me, it gives me all the attributes when I test my service using Postman! 我使用Hibernate创建一个客户端实体简单的Maven项目,它的DAO实现,我也有一个服务类ClientResource一些@POST@ GET方法,我想用@JsonView得到公正的名称和该标识客户端,但是注释对我不起作用,当我使用Postman测试服务时,它为我提供了所有属性!

Client entity 客户实体

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));
}

} }

Views 观看次数

    package Services;

public class Views {

    public static class Public { }

    public static class Internal extends Public { }


}

pom.xml 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>
package Services;

public class Views {

    public interface Public { }

    public interface Internal extends Public { }


}

make the following change in your views may help. 对您的看法进行以下更改可能会有所帮助。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM