question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SpringDataRest with multiple DTO-s

See original GitHub issue

Version: 3.0.0-SNAPSHOT

Generating swagger document from SpringDataRest, multiple DTO is generated in contract.

In https://github.com/csegedicsaba/springfox-poc project we have Person and City entities.

@Entity
@Table(name = "PERSON")
public class Person {
    @Id
    private Long personId;

    @ManyToOne
    @JoinColumn(name = "CITY_ID")
    private City city;


    public Long getPersonId() {
        return this.personId;
    }

    public void setPersonId(Long zarolasId) {
        this.personId = zarolasId;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }
}
@Entity
@Table(name = "CITY")
public class City {

    @Id
    private long cityId;


    public long getCityId() {
        return cityId;
    }

    public void setCityId(long cityId) {
        this.cityId = cityId;
    }
}

The contract (getting from http://localhost:8080/v2/api-docs) contains Person, Person_1 and Person_2. The generated contract is different sometimes when I restart the application (no change, only restart). In springfox-poc_api-docs_v1.json.txt Person_1 and Person_2 is generated and used. But in springfox-poc_api-docs_v2.json.txt Person_2 is generated but not used.

By adding the opposite relation to City only the Person is generated (no Person_1 or Person_2).

    @OneToMany(mappedBy = "city")
    private Collection<Person> persons;

    public Collection<Person> getPersons() {
        return persons;
    }

    public void setPersons(Collection<Person> persons) {
        this.persons = persons;
    }

We don’t want to add always both side of relationship because it is unnecessary.

springfox-poc_api-docs_v1.json.txt springfox-poc_api-docs_v2.json.txt

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
csegedicsabacommented, Feb 9, 2020

Thx @MaksimOrlov! We are waiting for merge…

1reaction
MaksimOrlovcommented, Feb 8, 2020

Here is the PR #3247 to fix this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to work with DTO in Spring Data REST projects?
Create methods return the projection: a single one, a list of DTO and a paged list of DTO. Annotation Relation is used when...
Read more >
Automatically Mapping DTO to Entity on Spring Boot APIs
By using DTOs and ModelMapper, we can provide as many different versions (with different structures) of our entities as we want.
Read more >
Spring Boot DTO Example - Entity To DTO Conversion
It is basically used to pass data with multiple attributes in one shot from client to server, to avoid multiple calls to a...
Read more >
Entity To DTO Conversion for a Spring REST API - Baeldung
2. Model Mapper. Let's start by introducing the main library that we're going to use to perform this entity-DTO conversion, ModelMapper.
Read more >
Spring Data REST Reference Guide
Reference Documentation. 2. Introduction. REST web services have become the number one means for application integration on the web. In its core ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found