spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
905 stars 558 forks source link

Json Boolean values are not being captured in the project #2351

Closed ved-asole closed 5 months ago

ved-asole commented 6 months ago

Hi Team,

The json boolean values are not being captured from the JSON body that was sent from the POST request. Could you please look into the below issue as soon as possible?

Below are the Details :

POJO :

@AllArgsConstructor
@NoArgsConstructor
@Validated
@Entity
@Table(name = "category")
public class Category {

    @Id
    @Column(updatable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "category_seq")
    @SequenceGenerator(name = "category_seq", allocationSize = 0)
    private long categoryId;

    @Column(nullable = false, length = 20)
    private String name;

    private String image;
    private String desc;

    @ManyToOne
    @JoinColumn(name = "parent_category_id")
    private Category parentCategory;

    @JsonProperty
    private boolean isActive;

    public boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(boolean isActive) {
        this.isActive = isActive;
    }

    ........other getters and setters.......

JSON Request :

Url : http://localhost:8000/categories Method : POST Request Body :

{
  "categoryId": 1,
  "name": "Top Offers",
  "image": "https://rukminim1.flixcart.com/fk-p-flap/64/64/image/085406bae47866d5.png?q=100",
  "desc": "Top Offers on Mobile Phones",
  "parentCategory":{},
  "isActive":true
}

Response from API :

{
    "name": "Top Offers",
    "image": "https://rukminim1.flixcart.com/fk-p-flap/64/64/image/085406bae47866d5.png?q=100",
    "desc": "Top Offers on Mobile Phones",
    "isActive": false,
    "_links": {
        "self": {
            "href": "http://localhost:8000/categories/1"
        },
        "category": {
            "href": "http://localhost:8000/categories/1"
        },
        "parentCategory": {
            "href": "http://localhost:8000/categories/1/parentCategory"
        }
    }
}
ved-asole commented 6 months ago

Please find below post for reference :

WHAT THE HELL IS WRONG WITH SPRING BOOT!? Can’t convert boolean to JSON correctly! + SOLUTION

odrotbohm commented 5 months ago

The type is not following the Java beans convention for primitive boolean. See more on this here. So generally speaking, this is a simple Jackson issue and has got nothing to do with either Boot or Spring Data REST.