Home > Mobile >  @PostMapping saving empty objects spring boot jpa gradle mysql db
@PostMapping saving empty objects spring boot jpa gradle mysql db

Time:01-12

I'm trying to make a crud app using SpringBoot JPA HIBERNATE GRADLE MYSQL. My issue is that when I hit an @PostMapping endpoint to save a user, an empty object get's saved in database. Only id is generated but the other column shows null. Also when I hit @GetMapping it returns empty object list . I'm using PostMan for requests.

Images of Postamn Requests

create

enter image description here

all

enter image description here

Mysql Database

enter image description here

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.1'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'db.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '19'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.mysql:mysql-connector-j'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

application.properties

spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.datasource.url=jdbc:mysql://localhost:3306/auto?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mainApplication.java

package db.example.autodbconnect;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AutodbconnectApplication {

    public static void main(String[] args) {
        SpringApplication.run(AutodbconnectApplication.class, args);
    }

}

UserEntity

package db.example.autodbconnect.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column
    private String name;
    
}

User JPA Repository

 package db.example.autodbconnect.repositories;

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

import db.example.autodbconnect.entities.User;

@Repository
public interface UserRepository extends JpaRepository<User,Long> {
    
}

User Service

package db.example.autodbconnect.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import db.example.autodbconnect.entities.User;
import db.example.autodbconnect.repositories.UserRepository;

@Service
public class UserService {
    
    @Autowired
    private UserRepository userRepo;


    public User create(User body) {
        return userRepo.save(body);

    }

    public List<User> findAll(){
        List<User> res=userRepo.findAll();
        return res;
    }
    
}

User Controller

package db.example.autodbconnect.contoller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import db.example.autodbconnect.entities.User;
import db.example.autodbconnect.service.UserService;

@RestController
@RequestMapping("/users")
public class UserController {
    
    @Autowired
    private UserService userService;

    @PostMapping("/create")
    public ResponseEntity<User> create(@RequestBody User body) {
        return  new ResponseEntity<User>(userService.create(body), HttpStatus.CREATED);
    }

    @GetMapping("/all")
    public ResponseEntity<List<User>> findALL(){
        return new ResponseEntity<List<User>>(userService.findAll(),HttpStatus.OK);
    }
}

CodePudding user response:

You have not added getter setter methods for each field that's why it is storing null values solution- 1-either add getter setter method for each field or 2-you can also add lombook dependency and use @Data annotation it will add getter setter method for you

Hope it will help you

  • Related