英文:
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);
}
它不仅要求name
和surname
,还要求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 = "/contacts", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Contact> 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("/contacts")
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 = "serial")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论