【问题标题】:@JsonView doesn't work with REST service@JsonView 不适用于 REST 服务
【发布时间】:2018-11-21 07:33:46
【问题描述】:

我创建了一个简单的 maven 项目,其中包含一个使用 hibernate 的 Client 实体及其 DAO 实现,我还有一个带有一些 @POSTClientResource /em> 和 @GET 方法,我想使用 @JsonView 来获取 nameid的客户端,但注释对我不起作用,当我使用 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() {
}

客户端资源

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>

【问题讨论】:

    标签: json hibernate rest maven jakarta-ee


    【解决方案1】:
    package Services;
    
    public class Views {
    
        public interface Public { }
    
        public interface Internal extends Public { }
    
    
    }
    

    在您的视图中进行以下更改可能会有所帮助。

    谢谢

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2015-08-07
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多