Home > other >  One single value returning null in POST request
One single value returning null in POST request

Time:03-15

I'm sending this JSON in my Postman:

{
    "restaurant": "Restaurant test",
    "address": "Address test",
    "website": "Website test",
    "description": "Descriptionn test"
}

And for some reason, in my H2 Console, just the restaurant value is returning null

H2 Console Output

I dont know exactly what is the problem, so I'm gonna show my files for Restaurant.java

Restaurant.java

package com.dbserver.restaurantes.entities;

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

@Entity
@Table(name = "tb_restaurants")
public class Restaurant {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String restaurant;
    private String address;
    private String website;
    private String description;

    public Restaurant() {
    }

    public Restaurant(Long id, String restaurant, String address, String website, String description) {
        this.id = id;
        this.restaurant = restaurant;
        this.address = address;
        this.website = website;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return restaurant;
    }

    public void setName(String restaurant) {
        this.restaurant = restaurant;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

RestaurantController.java

package com.dbserver.restaurantes.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.dbserver.restaurantes.dto.RestaurantDTO;
import com.dbserver.restaurantes.entities.Restaurant;
import com.dbserver.restaurantes.services.RestaurantServices;

@RestController
@RequestMapping(value = "/restaurants")
public class RestaurantController {

    @Autowired
    private RestaurantServices service;

    @GetMapping
    public Page<RestaurantDTO> findAll(Pageable pageable) {
        return service.findAll(pageable);
    }

    @GetMapping(value = "/{id}")
    public RestaurantDTO findById(@PathVariable Long id) {
        return service.findById(id);
    }

    @PostMapping
    public void addRestaurant(@RequestBody Restaurant newRestaurant) {
        service.addRestaurant(newRestaurant);
    }
}

RestaurantService.java

package com.dbserver.restaurantes.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dbserver.restaurantes.dto.RestaurantDTO;
import com.dbserver.restaurantes.entities.Restaurant;
import com.dbserver.restaurantes.repositories.RestaurantRepository;

@Service
public class RestaurantServices {

    @Autowired
    private RestaurantRepository repository;

    @Transactional(readOnly = true)
    public Page<RestaurantDTO> findAll(Pageable pageable) {
        Page<Restaurant> result = repository.findAll(pageable);
        Page<RestaurantDTO> page = result.map(x -> new RestaurantDTO(x));
        return page;
    }
    
    @Transactional(readOnly = true)
    public RestaurantDTO findById(Long id) {
        Restaurant result = repository.findById(id).get();
        RestaurantDTO dto = new RestaurantDTO(result);
        return dto;
    }
    
    @Transactional
    public Restaurant addRestaurant(Restaurant newRestaurant) {
        return repository.saveAndFlush(newRestaurant);
    }
}

RestaurantDTO.java

package com.dbserver.restaurantes.dto;

import com.dbserver.restaurantes.entities.Restaurant;

public class RestaurantDTO {
    private Long id;
    private String restaurant;
    private String address;
    private String website;
    private String description;

    public RestaurantDTO() {
    }

    public RestaurantDTO(Long id, String restaurant, String address, String website, String description) {
        this.id = id;
        this.restaurant = restaurant;
        this.address = address;
        this.website = website;
        this.description = description;
    }

    public RestaurantDTO(Restaurant restaurantDTO) {
        id = restaurantDTO.getId();
        restaurant = restaurantDTO.getName();
        address = restaurantDTO.getAddress();
        website = restaurantDTO.getWebsite();
        description = restaurantDTO.getDescription();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return restaurant;
    }

    public void setNome(String restaurant) {
        this.restaurant = restaurant;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

CodePudding user response:

Try to add getter and setter for restorant in your RestaurantDTO

  • Related