如何发送具有 2 个不同值的 json 文件?
How to send json file with 2 different values?
我发送 JSON 文件(现在来自邮递员)
body 是这样的:
{
"email": "com@com.com",
"password": "123456"
}
控制器上的方法是:
public Company getCompanyDetailsByEmailAndPassword(String email, String password) {
System.out.println("this is email: " +email); // added the sysout to understand what arrived.
System.out.println("this is password: " +password);
return companyRepository.findByEmailAndPassword(email, password);
}
我收到一个错误 -
this is email: {
"email": "com@com.com",
"password": "123456"
}
this is password: null
2022-04-04 18:14:07.830 DEBUG 6076 --- [nio-8080-exec-8] org.hibernate.SQL : select company0_.id as id1_0_, company0_.email as email2_0_, company0_.name as name3_0_, company0_.password as password4_0_ from companies company0_ where company0_.email=? and (company0_.password is null)
Hibernate: select company0_.id as id1_0_, company0_.email as email2_0_, company0_.name as name3_0_, company0_.password as password4_0_ from companies company0_ where company0_.email=? and (company0_.password is null)
2022-04-04 18:14:07.831 TRACE 6076 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [{
如何解决?
由于负载本身是一个字符串(采用 JSON 格式),因此电子邮件参数被分配给整个 JSON 文本。
我建议改用单个参数,其类型(可能命名为“帐户”之类的名称)包含电子邮件字段和密码字段。然后 Spring 知道两个 JSON 字段属于那个 class 的单个实例,并且您可以访问该实例的字段。
https://www.baeldung.com/spring-request-response-body#@requestbody 有一个可能有用的示例。
最简单的方法就是写一个简单的class
public class EmailIdHolder {
public String email;
public String password;
// add setters and getters here
}
将您的控制器方法更改为:
public Company getCompanyDetailsByEmailAndPassword(EmailIdHolder id) {
System.out.println("this is email: " +id.getEmail); // added the sysout to understand what arrived.
System.out.println("this is password: " +id.getPassword);
return companyRepository.findByEmailAndPassword(email, password);
}
我发送 JSON 文件(现在来自邮递员) body 是这样的:
{
"email": "com@com.com",
"password": "123456"
}
控制器上的方法是:
public Company getCompanyDetailsByEmailAndPassword(String email, String password) {
System.out.println("this is email: " +email); // added the sysout to understand what arrived.
System.out.println("this is password: " +password);
return companyRepository.findByEmailAndPassword(email, password);
}
我收到一个错误 -
this is email: {
"email": "com@com.com",
"password": "123456"
}
this is password: null
2022-04-04 18:14:07.830 DEBUG 6076 --- [nio-8080-exec-8] org.hibernate.SQL : select company0_.id as id1_0_, company0_.email as email2_0_, company0_.name as name3_0_, company0_.password as password4_0_ from companies company0_ where company0_.email=? and (company0_.password is null)
Hibernate: select company0_.id as id1_0_, company0_.email as email2_0_, company0_.name as name3_0_, company0_.password as password4_0_ from companies company0_ where company0_.email=? and (company0_.password is null)
2022-04-04 18:14:07.831 TRACE 6076 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [{
如何解决?
由于负载本身是一个字符串(采用 JSON 格式),因此电子邮件参数被分配给整个 JSON 文本。
我建议改用单个参数,其类型(可能命名为“帐户”之类的名称)包含电子邮件字段和密码字段。然后 Spring 知道两个 JSON 字段属于那个 class 的单个实例,并且您可以访问该实例的字段。
https://www.baeldung.com/spring-request-response-body#@requestbody 有一个可能有用的示例。
最简单的方法就是写一个简单的class
public class EmailIdHolder {
public String email;
public String password;
// add setters and getters here
}
将您的控制器方法更改为:
public Company getCompanyDetailsByEmailAndPassword(EmailIdHolder id) {
System.out.println("this is email: " +id.getEmail); // added the sysout to understand what arrived.
System.out.println("this is password: " +id.getPassword);
return companyRepository.findByEmailAndPassword(email, password);
}