Home > Software design >  Database entry problem. Only null value is written
Database entry problem. Only null value is written

Time:10-06

I'm trying to work with REST api and after creating JSON post call, null value is written to the database instead of the value that was written in JSON.

I am working with PostgreSQL database.

After I send JSON using Postman or Swagger I get Server Response Code: 200. But the database writes the color as null.

EXAMPLE:

JSON:

{
  "color": "string"
}

Server response:

{
  "id": 2,
  "color": null
}
 connection: keep-alive 
 content-type: application/json 
 date: Thu06 Oct 2022 13:27:09 GMT 
 keep-alive: timeout=60 
 transfer-encoding: chunked 

I added here also application.properties file if I accidentally made some mistake already directly in it.

Project structure:

  • controller
  • dto
  • model
  • repository
  • service

application.properties

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/democalendareventapp
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.generate-dll=true
spring.datasource.hikari.maximumPoolSize=2

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

model: EventCategoryColor

package com.radocode.DemoCalendarEventApp.model;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@NoArgsConstructor
@Data
@Table(name = "EventCategoryColor")
public class EventCategoryColor {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String color;

    public EventCategoryColor(String color) {
        this.color = color;
    }
}

repository: EventCategoryColorRepository

package com.radocode.DemoCalendarEventApp.repository;

import com.radocode.DemoCalendarEventApp.model.EventCategoryColor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EventCategoryColorRepository extends CrudRepository<EventCategoryColor, Long> {
}

service - Interface: EventCategoryColorService

package com.radocode.DemoCalendarEventApp.service;

import com.radocode.DemoCalendarEventApp.dto.requestDto.EventCategoryColorRequestDto;
import com.radocode.DemoCalendarEventApp.model.EventCategoryColor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface EventCategoryColorService {
    public EventCategoryColor addColor(EventCategoryColorRequestDto eventCategoryColorRequestDto);
    public List<EventCategoryColor> getColors();
    public EventCategoryColor getColor(Long id);
    public EventCategoryColor deleteColor(Long id);
    public EventCategoryColor editColor(Long id, EventCategoryColorRequestDto eventCategoryColorRequestDto);
}

service: EventCategoryColorServiceImp

package com.radocode.DemoCalendarEventApp.service;

import com.radocode.DemoCalendarEventApp.dto.requestDto.EventCategoryColorRequestDto;
import com.radocode.DemoCalendarEventApp.model.EventCategoryColor;
import com.radocode.DemoCalendarEventApp.repository.EventCategoryColorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class EventCategoryColorServiceImpl implements EventCategoryColorService {

    private final EventCategoryColorRepository eventCategoryColorRepository;

    @Autowired
    public EventCategoryColorServiceImpl(EventCategoryColorRepository eventCategoryColorRepository) {
        this.eventCategoryColorRepository = eventCategoryColorRepository;
    }

    @Override
    public EventCategoryColor addColor(EventCategoryColorRequestDto eventCategoryColorRequestDto) {
        EventCategoryColor eventCategoryColor = new EventCategoryColor();
        eventCategoryColor.setColor(eventCategoryColor.getColor());
        return eventCategoryColorRepository.save(eventCategoryColor);
    }

    @Override
    public List<EventCategoryColor> getColors() {
        List<EventCategoryColor> colors = new ArrayList<>();
        eventCategoryColorRepository.findAll().forEach(colors::add);
        return colors;
    }

    @Override
    public EventCategoryColor getColor(Long id) {
        return eventCategoryColorRepository.findById(id).orElseThrow(() ->
                new IllegalArgumentException("Color with Id: "   id   ", could not be found"));
    }

    @Override
    public EventCategoryColor deleteColor(Long id) {
        EventCategoryColor eventCategoryColor = getColor(id);
        eventCategoryColorRepository.delete(eventCategoryColor);
        return eventCategoryColor;
    }

    @Transactional
    @Override
    public EventCategoryColor editColor(Long id, EventCategoryColorRequestDto eventCategoryColorRequestDto) {
        EventCategoryColor colorToEdit = getColor(id);
        colorToEdit.setColor(eventCategoryColorRequestDto.getColor());
        return colorToEdit;
    }
}

dto: EventCategoryColorRequestDto

package com.radocode.DemoCalendarEventApp.dto.requestDto;

import lombok.Data;

@Data
public class EventCategoryColorRequestDto {
    private String color;
}

controller: EventCategoryColorController

package com.radocode.DemoCalendarEventApp.controller;

import com.radocode.DemoCalendarEventApp.dto.requestDto.EventCategoryColorRequestDto;
import com.radocode.DemoCalendarEventApp.model.EventCategoryColor;
import com.radocode.DemoCalendarEventApp.service.EventCategoryColorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/event-category-color")
public class EventCatogeryColorController {

    private final EventCategoryColorService eventCategoryColorService;

    @Autowired
    public EventCatogeryColorController(EventCategoryColorService eventCategoryColorService) {
        this.eventCategoryColorService = eventCategoryColorService;
    }

    @PostMapping("/add")
    public ResponseEntity<EventCategoryColor> addColor(
            @RequestBody final EventCategoryColorRequestDto eventCategoryColorRequestDto) {
        EventCategoryColor eventCategoryColor = eventCategoryColorService.addColor(eventCategoryColorRequestDto);
        return new ResponseEntity<>(eventCategoryColor, HttpStatus.OK);
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<EventCategoryColor> getColorById(@PathVariable final Long id) {
        EventCategoryColor eventCategoryColor = eventCategoryColorService.getColor(id);
        return new ResponseEntity<>(eventCategoryColor, HttpStatus.OK);
    }

    @GetMapping("/get-all")
    public ResponseEntity<List<EventCategoryColor>> getColors() {
        List<EventCategoryColor> eventCategoryColors = eventCategoryColorService.getColors();
        return new ResponseEntity<>(eventCategoryColors, HttpStatus.OK);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<EventCategoryColor> deleteColor(@PathVariable final Long id) {
        EventCategoryColor eventCategoryColor = eventCategoryColorService.deleteColor(id);
        return new ResponseEntity<>(eventCategoryColor, HttpStatus.OK);
    }

    @PutMapping("/edit/{id}")
    public ResponseEntity<EventCategoryColor> editColor(
            @PathVariable final Long id,
            @RequestBody final EventCategoryColorRequestDto eventCategoryColorRequestDto) {
        EventCategoryColor eventCategoryColor = eventCategoryColorService.editColor(id, eventCategoryColorRequestDto);
        return new ResponseEntity<>(eventCategoryColor, HttpStatus.OK);
    }
}

CodePudding user response:

You set theo color value from the newly created entity to the entity which is not initialized at this time:

@Override
public EventCategoryColor addColor(EventCategoryColorRequestDto eventCategoryColorRequestDto) {
    EventCategoryColor eventCategoryColor = new EventCategoryColor();
    eventCategoryColor.setColor(eventCategoryColor.getColor());
    return eventCategoryColorRepository.save(eventCategoryColor);
}

should be:

@Override
public EventCategoryColor addColor(EventCategoryColorRequestDto eventCategoryColorRequestDto) {
    EventCategoryColor eventCategoryColor = new EventCategoryColor();
    eventCategoryColor.setColor(eventCategoryColorRequestDto.getColor());
    return eventCategoryColorRepository.save(eventCategoryColor);
}
  • Related