Home > Back-end >  Spring Boot MySQL - Why GET works but POST returns error?
Spring Boot MySQL - Why GET works but POST returns error?

Time:07-06

I created a Bookings class (Model Class) for the table "Bookings" in my MySQL DB, All the data members in this class are set in accordance with the database table.

All the GET requests work, only the POST requests return error.

Repository Interface :

package com.example.demo.bookings;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookingsRepository extends CrudRepository<Bookings, Integer>{

    Iterable<Bookings> findAllByBookingName(String name);

}

BookingsController class :

package com.example.demo.bookings;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BookingsController {
    
    @Autowired
    BookingsService bs;
    
    @RequestMapping("/bookings")
    public List<Bookings> getBookings(){
        return bs.getAllBookings();
    }
    
    @RequestMapping("/{name}/bookings")
    public List<Bookings> getBookings(@PathVariable String name){
        return bs.getBookingsByName(name);
    }
    
    @PostMapping("/bookings")
    public void addBookings(Bookings bookings) {
        bs.addBookings(bookings);
    }
}

Bookings POJO class :

package com.example.demo.bookings;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Bookings {
    @Id
    int bookingId;
    String bookingName;
    int bookingPid;
    int passengers;
    int cost;
    
    public Bookings() {
        
    }

    public Bookings(int bookingId, String bookingName, int bookingPid, int passengers, int cost) {
        super();
        this.bookingId = bookingId;
        this.bookingName = bookingName;
        this.bookingPid = bookingPid;
        this.passengers = passengers;
        this.cost = cost;
    }

    public int getBookingId() {
        return bookingId;
    }

    public void setBookingId(int bookingId) {
        this.bookingId = bookingId;
    }

    public String getBookingName() {
        return bookingName;
    }

    public void setBookingName(String bookingName) {
        this.bookingName = bookingName;
    }

    public int getBookingPid() {
        return bookingPid;
    }

    public void setBookingPid(int bookingPid) {
        this.bookingPid = bookingPid;
    }

    public int getPassengers() {
        return passengers;
    }

    public void setPassengers(int passengers) {
        this.passengers = passengers;
    }

    public int getCost() {
        return cost;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }
    
    
}

Edit 2:

As suggested, I changed the Model Class datamembers to camelcase for columns having '_' in their name. This helped resolve much of the problems I was facing but the POST method still returns error.

The error I get when I try a POST request :

java.sql.SQLIntegrityConstraintViolationException: Column 'booking_name' cannot be null

CodePudding user response:

In your TopicService you are trying to autowire TopicsRepository, but you don't have any bean of type TopicsRepository.

Add following annotation:

@Repository
public interface TopicsRepository extends CrudRepository<Topics, String>{

}

CodePudding user response:

The underscore-separated fields in the database are usually mapped to camelCase fields in the classes. The field name in the Bookings class should be bookingName, and the repository method signature should be:

Iterable<Bookings> findAllByBookingName(String bookingName);

Same for all other fields in the Bookings class.

  • Related