如何使用来自Postman的JSON将值存储在特定列中(值由下划线_分隔):

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

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&lt;String,String&gt; or create a DTO and then store value in String datatype like this

String data=map.get(&quot;EMPLOYEE_DETAILS&quot;);
or
String data=employeeDto.getEmployesDetails();

First split data by using _(underscore) like this

String splitter=data.split(&quot;_&quot;);

create all argument constructor in Employee enitity and pass by splitter like this

Employee employee =new Employee(splitter[0],splitter[1],splitter[2])

Thanks

huangapple
  • 本文由 发表于 2020年3月16日 20:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/60706121.html
匿名

发表评论

匿名网友

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

确定