什么对象应该在Spring Boot REST API中进行POST请求?

huangapple go评论154阅读模式
英文:

What object should I POST in Spring Boot REST API?

问题

以下是翻译好的部分:

我有一个带有id、name和surname的Hibernate模型。我正在使用它从数据库中获取数据,然后在GET端点中是这样的:

@GetMapping(value = "/contacts", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Contact> allContacts() {
    return contactService.findAll();
}

正如你所看到的,它返回了Contact对象。实际上,它是一个Hibernate实体。

问题是,当我使用这段代码时:

@PostMapping("/contacts")
public Contact createContact(Contact contact) {
    return contactService.createContact(contact);
}

它不仅要求namesurname,还要求id。POST方法不应该要求id,因为它们还没有被创建。我应该怎么做才能让它不要求id

编辑:以下是Contact.java类的内容

import lombok.Data;
import javax.persistence.*;

@Entity
@Data
public class Contact {

    public Contact() {
    }

    public Contact(Integer id, String name, String surname) {
        this.id = id;
        this.name = name;
        this.surname = surname;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private Integer id;

    private String name;

    private String surname;

}
英文:

I have a Hibernate model with id, name and surname. I am using it to get data from the database and then at the GET end-point is this one:

@GetMapping(value = &quot;/contacts&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
public List&lt;Contact&gt; allContacts() {
    return contactService.findAll();
}

As you can see it returns the Contact object. Actually it is a Hibernate entity.

The problem is that when I use this code

@PostMapping(&quot;/contacts&quot;)
public Contact createContact(Contact contact) {
    return contactService.createContact(contact);
}

it asks not only name and surname but also the id. POST methods should not ask for id since they are not created yet. What should I do so that it doesn't ask for an id?

Edit: Here is the Contact.java class

import lombok.Data;
import javax.persistence.*;

@Entity
@Data
public class Contact {

    public Contact() {
    }

    public Contact(Integer id, String name, String surname) {
        this.id = id;
        this.name = name;
        this.surname = surname;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = &quot;serial&quot;)
    private Integer id;

    private String name;

    private String surname;

}

答案1

得分: 1

定义一个 ContactInput 类,只包含用户需要输入的属性,然后创建一些映射代码,根据 ContactInput 创建一个有效的 Contact

英文:

Define a ContactInput class that only has the attributes you want the user to input and then create some mapping code that creates a valid Contact based on the ContactInput.

答案2

得分: 1

你应该创建一个ContactDto类。

@Data
public class ContactDto {
    private String name;
    private String surname;
}

@PostMapping中,你将会从用户那里获取ContactDto。你不能直接将ContactDto保存到数据库中。因此,你需要将ContactDto映射到Contact对象。你可以简单地创建一个ContactMapper类。

public class ContactMapper {
    public static Contact contactDtoToEntity(ContactDto dto) {
        Contact dbContact = new Contact();
        dbContact.setName(dto.getName());
        dbContact.setSurname(dto.getSurname());
        return dbContact;
    }
}

在服务层中保存联系人到数据库之前,你需要进行映射然后再进行保存。在数据库中,ID会被生成。

英文:

You should create ContactDto class

   @Data
   public ContactDto class {
    private String name;
    private String surname;
   }

In @PostMapping you are gonna get ContactDto from the user. You cannot saved ContactDto into your database. So you need to map ContactDto to Contact. What you can do is simply create ContractMapper class.

public static contactDtoToEntity(ContactDto dto){
Contact dbContact = new Contact();
dbContact.setName(dto.getName());
dbContact.setSurname(dto.getSurname());
return dbContact;
}

Before you saved the contact in your database in service layer, you need to map it and then save. Id is gonna be generated in the database.

huangapple
  • 本文由 发表于 2020年10月28日 05:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64562918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定