1

I have a problem with @GetMapping in Spring boot.

It's about my @GetMapping function that doesn't serialize my id on this model while getting all data from database:

//User.java
@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

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

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "joined_date")
    @CreatedDate
    private Date joinedDate;

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

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

    @Column(name = "email")
    private String email;
}

I tried many ways but cannot solve at all. Even this question: Spring boot @ResponseBody doesn't serialize entity id

This is the image below:

The ids doesn't serialized in json

Higanbana
  • 489
  • 10
  • 26

3 Answers3

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

One solution is to use Integer wrapper class instead of the int. int default value is 0 and Integer's is null.

Muhammad Waqas
  • 367
  • 3
  • 13
2

You have to use @PathVariable in your controller. You can try this:

Entity:

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

Controller:

@GetMapping("/users/{id}")
public ResponseEntity<User> getUserFromId(@PathVariable int id) {
    System.out.println(id); // should display the id
    // ...
}
veben
  • 19,637
  • 14
  • 60
  • 80
  • Yeah I put that. Make sure to check the comment from the top of the topic – Higanbana Feb 08 '19 at 10:57
  • The problem is when I run the code and get data from URL(JSON), they don't have `Id`s but my database has. I assure that I add the `@PathVariable` before run the code – Higanbana Feb 08 '19 at 11:29
0

I did it! Because I forgot to put the getter/setter for the model. And there are more potentials here I wanna tell:

And there is the full form of the model:

package com.harrycoder.weebjournal.user;

import java.util.Date;

import javax.persistence.*;

import org.springframework.data.annotation.CreatedDate;

import com.fasterxml.jackson.annotation.*;

@Entity
@Table(name = "users")
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, 
        allowGetters = true)
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

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

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "joined_date")
    @CreatedDate
    private Date joinedDate;

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

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

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

    public Integer getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getBio() {
        return bio;
    }

    public String getEmail() {
        return email;
    }

    public Date getJoinedDate() {
        return joinedDate;
    }
}   
Higanbana
  • 489
  • 10
  • 26