Home > Enterprise >  rest api return duplicate values in postman and spring boot h2
rest api return duplicate values in postman and spring boot h2

Time:05-08

I am new spring boot developer and i am trying to develope and rest api . when I do it ,I get and issues that my api return two duplicated response in postman .But i haven't code anythiong to get duplicated valuese in my code . the one of duplicate values is my model clase variable and athor one is table's attribute name .

below response in postman

enter image description here

model class

public class person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY )
    private Long id;



    @Column(name = "name")
 
    private      String Name ;

    @Column(name ="surname")
 
    private   String Surname;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getSurname() {
        return Surname;
    }

    public void setSurname(String surname) {
        Surname = surname;
    }

}

repository

@Repository
public interface personRepository extends JpaRepository<person,Long> {
}

controller

@RestController
@RequestMapping("/person")
public class personController {

    @Autowired
    private personRepository repository;

    public personController(personRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/view/list/person")
    private List<person> viewperson() {
        return repository.findAll();
    }

    @PostMapping("/insert/person")
    private person savePerson(@RequestBody person obj) {

        return repository.save(obj);
    }

    @DeleteMapping("/delete/{id}")
    private void delete(@PathVariable Long id) {

        repository.deleteById(id);
    }
}

application.properties

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialec

t

CodePudding user response:

The problem is that you're not following the proper conventions in your naming strategy.

Due to this, Jackson doesn't know that your getters (getSurname(), getName()) are referencing the fields Surname and Name. That's why it serializes both your fields and your getters separately to JSON.

To fix this, you can follow the Java naming conventions and use a lowercase letter for the first character of your fields.

For example:

@Column(name = "name")
private String name; // Change this

@Column(name ="surname")
private String surname; // Change this

This will change your JSON output to:

{
  "id": 1,
  "name": "bryan",
  "surname": "Nicky"
}

If you want to keep your JSON with capital letters, you can use the @JsonProperty annotation:

@JsonProperty("Name") // Add this
@Column(name = "name")
private String name;

@JsonProperty("Surname") // Add this
@Column(name ="surname")
private String surname;

Unrelated to your question, but according to those naming conventions, your classes should start with a capital (eg. Person, PersonController, PersonRepository, ...).

  • Related