在 POST 请求中返回 null 的单个值

One single value returning null in POST request

我正在通过我的 Postman 发送这个 JSON:

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

出于某种原因,在我的 H2 控制台中,只有餐厅值返回 null

H2 Console Output

我不知道到底是什么问题,所以我要展示我的 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;
    }

}

尝试为您的餐厅添加 getter 和 setter TO