1

При создании нового product'а мне понадобилось получить его название, но, при использовании метода getName(), я получаю например: Product@11123.

@RestController
@RequestMapping("/product")
public class ProductController {
    private final ProductRepository productRepository;

    @Autowired
    public ProductController(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    // GET list items
    @GetMapping("")
    public Iterable<Product> readAll() {
        return productRepository.findAll();
    }

    @GetMapping("/{id}")
    public Product read(@PathVariable int id) {
        return productRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Not found"));
    }

    @PostMapping("")
    public Product create(@RequestBody Product newProduct) {
        newProduct.setModified(Calendar.getInstance().getTime());
        String name = newProduct.getName();
        return productRepository.save(newProduct);
    }

Сущность:

@Entity
@Table(name="products")
public class Product implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @NotNull
    private int id;

    @NotNull
    @Size(max=50)
    private String name;

    @NotNull
    @Size(max=150)
    private String description;

    @NotNull
    @Min(value = 0, message = "Must bigger than 0")
    @Max(value = Long.MAX_VALUE, message = "Must less than " + Long.MAX_VALUE)
    @Digits(integer = Integer.MAX_VALUE, fraction = 3)
    private double price;

    @NotNull
    @Min(value = 0)
    @Max(value = Long.MAX_VALUE, message = "Mus less than " + Long.MAX_VALUE)
    @Digits(integer = Integer.MAX_VALUE, fraction = 0)
    private int quantity = 0;

    @NotNull
    @Size(max = 150)
    private String thumbnail;

    @Column(name="is_valid")
    @NotNull
    private boolean isValid = true;

    @Temporal(TemporalType.TIMESTAMP)
    private Date modified;

    @ManyToOne
    @JoinColumn(name="category_id")
    private Category category;

    @ManyToOne
    @JoinColumn(name="supplier_id")
    private Supplier supplier;

    public Product(@NotNull @Size(max=50) String name, @NotNull @Size(max=150) String description, @NotNull @Min(value=0, message="Must bigger than 0") @Max(value=Long.MAX_VALUE, message="Must less than " + Long.MAX_VALUE) @Digits(integer=Integer.MAX_VALUE, fraction=3) double price, @NotNull @Min(value=0) @Max(value=Long.MAX_VALUE, message="Mus less than " + Long.MAX_VALUE) @Digits(integer=Integer.MAX_VALUE, fraction=0) int quantity, @NotNull @Size(max=150) String thumbnail, @NotNull boolean isValid, Date modified, Category category, Supplier supplier) {
        this.name=name;
        this.description=description;
        this.price=price;
        this.quantity=quantity;
        this.thumbnail=thumbnail;
        this.isValid=isValid;
        this.modified=modified;
        this.category=category;
        this.supplier=supplier;
    }

    public Product() {
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity=quantity;
    }

    public String getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(String thumbnail) {
        this.thumbnail=thumbnail;
    }

    public boolean isValid() {
        return isValid;
    }

    public void setValid(boolean valid) {
        isValid=valid;
    }

    public Date getModified() {
        return modified;
    }

    public void setModified(Date modified) {
        this.modified=modified;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category=category;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier=supplier;
    }
}

JSON:

{
    "id": 1,
    "name": "444",
    "description": "44",
    "price": 44,
    "quantity": 44,
    "thumbnail": "https://cdn.tgdd.vn/Products/Images/42/179673/huawei-nova-3i-trang-chipu-400x400.jpg",
    "modified": 1557835543418,
    "category": {
        "id": 1,
        "name": "Смартфоны",
        "modified": 1557835525109,
        "valid": true
    },
    "supplier": {
        "id": 1,
        "name": "qwe",
        "company": "qwe",
        "address": "qwe",
        "city": "qwe",
        "phone": "213213123",
        "fax": "23131231231",
        "modified": 213123,
        "valid": true
    },
    "valid": false
}
16
  • а какое там у name на самом деле значение? 14 мая 2019 в 13:53
  • Любое, но пусть будет Smartphone.
    – astatico
    14 мая 2019 в 13:55
  • а json добавьте плз 14 мая 2019 в 13:56
  • Подождите, пожалуйста
    – astatico
    14 мая 2019 в 13:57
  • Мда, плохо, ничего не выглядит подозрительно, а какой десериализатор? можете завести toString и вывести его при конструировании объекта? 14 мая 2019 в 14:09

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.