@RequestBody is returning null JSON with successful HTTP response

Viewed 138

I'm using Java Spring for my API and have built a React front end. I'm able to to make a PUSH request, that will create a new row in my SQL database. The problem is all the fields are null. Even using Postman to make the PUSH request causes null fields, so I know it's not the front end. I rebuilt all the getters and setters, double checked the imports, and made sure it was accepting JSON. Still no luck.

Here's my Controller:

@RestController
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping("api/items")
public class ItemController {
@Autowired
private ItemRepository itemRepository;

@GetMapping
public List<Item> getAllItems(){
    return itemRepository.findAll();
}

@PostMapping(consumes = "application/json")
public Item createItem(@RequestBody Item item){
    System.out.println(item);
    return itemRepository.save(item);
}}

Here's the Object:

@Entity
public class Item {
@Id
@GeneratedValue
private Long id;

private String itemName;

private String itemDesc;

private String itemLoc;

@Temporal(TemporalType.DATE)
private Calendar itemExp;

public Item() {
}

public Item(String name, String desc, String 
itemLoc, Calendar itemExp){
    this.itemName = name;
    this.itemDesc = desc;
    this.itemLoc = itemLoc;
    this.itemExp = itemExp;
}
public Long getId() {
    return id;
}

public String getItemName() {
    return itemName;
}

public void setItemName(String itemName) {
    this.itemName = itemName;
}

public String getItemDesc() {
    return itemDesc;
}

public void setItemDesc(String itemDesc) {
    this.itemDesc = itemDesc;
}

public String getItemLoc() {
    return itemLoc;
}

public void setItemLoc(String itemLoc) {
    this.itemLoc = itemLoc;
}

public Calendar getItemExp() {
    return itemExp;
}

public void setItemExp(Calendar itemExp) {
    this.itemExp = itemExp;
}

The JSON I'm putting Through:

 {"itemName":"Some Name","itemDesc":"Some Desc","itemLoc":"Some 
  LOC","itemExp":"2022-05-18"}
1 Answers

Your Json format is not correct.Should use double quotes Please try with this json

{ "itemName": "Some name", "itemDesc": "A description", "itemLoc": "The Location", "itemExp": "2022-05-10" }

Screenshot test done using Postman Screenshot test done using Postman

Related