【问题标题】:Java Set in Object giving empty JSON array对象中的 Java 集给出空 JSON 数组
【发布时间】:2020-07-14 04:20:33
【问题描述】:

我最近将我的项目从 Spring boot 1.2.4 升级到了 2.0.0。但是,我从下面给出的控制器收到的响应会为应该映射到 dto 对象中的 Set webappLocales 的部分生成一个空的 json 数组。我提出了一些打印语句,以检查正确填充的内容。事实上,内部的 ProjectDTO 也有一些 Set 字段,它们被正确转换为 json 数组。您可以在下面找到输出的 sn-p 和相应的代码。

DTO

public class WebappDTO {

    private long id;

    private String name;

    private Locale coreLocale;

    private Set<WebappLocale> webappLocales;

    private List<ProjectDTO> projects;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Locale getCoreLocale() {
        return coreLocale;
    }

    public void setCoreLocale(Locale coreLocale) {
        this.coreLocale = coreLocale;
    }

    public Set<WebappLocale> getWebappLocales() {
        return webappLocales;
    }

    public void setWebappLocales(Set<WebappLocale> webappLocales) {
        for (WebappLocale single: webappLocales) {
            System.out.println("-----INNER TEST TEST----" + single.getLocale().toString());
        }
        this.webappLocales = webappLocales;
    }

    public List<ProjectDTO> getProjects() {
        return projects;
    }

    public void setProjects(List<ProjectDTO> projectdto) {
        this.projects = projectdto;
    }

}

REST 控制器

@ApiOperation(value = "Get webapps for current user")
@RequestMapping(value = "/getAll", method = RequestMethod.GET, produces = {
        "application/json" })
@PostFilter("hasPermission(filterObject.getId(), 'com.adobe.onyx.server.model.Webapp', 'read')")
public List<WebappDTO> fetchAll() {
    List<ProjectDTO> projectDTO;
    Set<ProjectConfig> projects;
    List<WebappDTO> webappDTO = new ArrayList<WebappDTO>();

    List<Webapp> webapps = getAllWebapps();

    for (Webapp w : webapps) {

        WebappDTO wdto = new WebappDTO();
        wdto.setId(w.getId());
        wdto.setName(w.getName());
        wdto.setCoreLocale(w.getCoreLocale());
        wdto.setWebappLocales(w.getWebappLocales());
        projects = w.getProjectConfig();
        projectDTO = new ArrayList<ProjectDTO>();

        for (ProjectConfig p : projects) {
            ProjectDTO pd = new ProjectDTO();
            pd.setId(p.getId());
            pd.setAlfProject(p.getAlfProject());
            pd.setMockBuildAvailable(p.isMockBuildAvailable());
            pd.setMockElementOrder(p.getMockElementOrder());
            pd.setMockPrefix(p.getMockPrefix());
            pd.setMockSuffix(p.getMockSuffix());
            pd.setName(p.getName());
            pd.setPlaceholderRegex(p.getPlaceholderRegex());
            pd.setPostProcessors(p.getPostProcessors());
            pd.setProjectStartDate(p.getProjectStartDate());
            pd.setJiraIssueTypeId(p.getJiraIssueTypeId());
            pd.setJiraPriorityId(p.getJiraPriorityId());
            pd.setJiraProjectId(p.getJiraProjectId());
            pd.setBugAssignee(p.getBugAssignee());
            projectDTO.add(pd);
        }
        wdto.setProjects(projectDTO);
        webappDTO.add(wdto);

        Set<WebappLocale> test = wdto.getWebappLocales();
        for (WebappLocale single: test) {
            System.out.println("-----OUTER TEST TEST----" + single.getLocale().toString());
        }

    }
    return webappDTO;
}       

Web 应用程序区域设置 DTO

@Entity
@Table(name = "webapp_locales", uniqueConstraints = { 
@UniqueConstraint(columnNames = {
    "locale_id", "webapp_id" }) })
@XmlRootElement
public class WebappLocale implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;

@ManyToOne
@XmlTransient
@JsonIgnore
@JoinColumn(name = "locale_id")
private Locale locale;

@ManyToOne
@XmlTransient
@JsonIgnore
@JoinColumn(name = "webapp_id")
private Webapp webapp;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Locale getLocale() {
    return locale;
}

public void setLocale(Locale locale) {
    this.locale = locale;
}

public Webapp getWebapp() {
    return webapp;
}

public void setWebapp(Webapp webapp) {
    this.webapp = webapp;
}
}

【问题讨论】:

  • WebappLocale 长什么样子?
  • @SimonMartinelli 我已经添加了相同的内容供您参考
  • 所有字段都有JsonIgnore。你认为应该是什么结果?
  • 我只是问自己,为什么要从一个古老的不受支持的版本升级到一个旧的不受支持的框架版本? github.com/spring-projects/spring-boot/wiki/Supported-Versions
  • 所以之前我们得到的 JSON 对象只包含 id 的

标签: java spring spring-boot jackson


【解决方案1】:

升级似乎取消了 REST 响应中的 Id 字段属性。我找到了一些相关的答案here。以下是我为解决此问题而遵循的解决方案。求原解here

import java.util.stream.Collectors;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;

@Configuration
public class MyRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {

    @Autowired
    private EntityManager entityManager;

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(entityManager.getMetamodel().getEntities().stream().map(e -> e.getJavaType()).collect(Collectors.toList()).toArray(new Class[0]));
    }

}

【讨论】:

    猜你喜欢
    • 2012-08-21
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2023-03-29
    • 2018-08-28
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多