Home > Enterprise >  Spring Boot REST API MySQL Project - Internal Server Error, Status 500
Spring Boot REST API MySQL Project - Internal Server Error, Status 500

Time:07-06

Edit :

Found out the problem...

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.

The problem occurs when I try to find all the bookings for a "booking_name", Below are the BookingsRepository and Bookings class respectively,

package com.example.demo.bookings;

import org.springframework.data.jpa.repository.JpaRepository;

public interface BookingsRepository extends JpaRepository<Bookings, Integer>{

    Iterable<Bookings> findAllByBooking_Name(String booking_name);

}
package com.example.demo.bookings;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Bookings {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int booking_id;
    String booking_name;
    int booking_pid;
    int passengers;
    double cost;
    
    public Bookings() {
        
    }
    
    public Bookings(int booking_id, String booking_name, int booking_pid, int passengers, double cost) {
        super();
        this.booking_id = booking_id;
        this.booking_name = booking_name;
        this.booking_pid = booking_pid;
        this.passengers = passengers;
        this.cost = cost;
    }

    public int getBooking_id() {
        return booking_id;
    }

    public void setBooking_id(int booking_id) {
        this.booking_id = booking_id;
    }

    public String getBooking_name() {
        return booking_name;
    }

    public void setBooking_name(String booking_name) {
        this.booking_name = booking_name;
    }

    public int getBooking_pid() {
        return booking_pid;
    }

    public void setBooking_pid(int booking_pid) {
        this.booking_pid = booking_pid;
    }

    public int getPassengers() {
        return passengers;
    }

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

    public double getCost() {
        return cost;
    }

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

I tried changing the field "booking_name" to "bookingName" but it randomizes the order of the fields in the JSON output, Hence the POST method returns errors.

Here's the error I get

Error creating bean with name 'bookingsController': Unsatisfied dependency expressed through field 'bs'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookingsService': Unsatisfied dependency expressed through field 'bookingRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookingsRepository' defined in com.example.demo.bookings.BookingsRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.lang.Iterable com.example.demo.bookings.BookingsRepository.findAllByBooking_Name(java.lang.String); Reason: Failed to create query for method public abstract java.lang.Iterable com.example.demo.bookings.BookingsRepository.findAllByBooking_Name(java.lang.String)! No property 'booking' found for type 'Bookings'; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Iterable com.example.demo.bookings.BookingsRepository.findAllByBooking_Name(java.lang.String)! No property 'booking' found for type 'Bookings'

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