英文:
How to Store values in specific column by using JSON from Postman (values are separated by underscore_)
问题
以下是翻译好的内容:
任何人都可以帮助我吗?使用@RequestBody
将数据从Postman以下面默认的JSON格式进行发布是很容易的:
{"id": 1, "firstName": "abc", "lastName": "xyz"}
但是如何以这种格式发布数据呢?
{"EMPLOYEE_DETAILS": "1_abc_xyz"}
并且数据将被存储在特定的列中。
@Entity
@Table(name = "employee")
public class Employee {
@Id
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
// 生成setter和getter方法
}
英文:
Anybody please can help me it's easy to post data from Postman in below default JSON format {"id":1, "firstName":"abc", "lastName":"xyz"}
by using @RequestBody
Employee emp
but how to post data in this format {"EMPLOYEE_DETAILS":"1_abc_xyz"}
And data will be stored in specific column
@Entity
@Table(name="employee")
public class Employee{
@Id private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
//Generate setters and getters
}
答案1
得分: 1
你可以在控制器中以 Map<String, String>
的形式获取输入,或者创建一个 DTO
,然后将值存储在 String 数据类型中,就像这样:
String data = map.get("EMPLOYEE_DETAILS");
或者
String data = employeeDto.getEmployesDetails();
首先,使用下划线(_)将数据拆分,就像这样:
String[] splitter = data.split("_");
在 Employee 实体中创建带有所有参数的构造函数,然后像这样传递 splitter
:
Employee employee = new Employee(splitter[0], splitter[1], splitter[2]);
谢谢。
英文:
You can take input in controller either Map<String,String>
or create a DTO
and then store value in String datatype like this
String data=map.get("EMPLOYEE_DETAILS");
or
String data=employeeDto.getEmployesDetails();
First split data by using _(underscore) like this
String splitter=data.split("_");
create all argument constructor in Employee enitity and pass by splitter
like this
Employee employee =new Employee(splitter[0],splitter[1],splitter[2])
Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论