我是SpringBoot的新手。
我有2个对象
public class Phone {
private long id;
private String phone;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
public class MailID {
private long id;
private String mail;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
我已经创建了一个控制器来获取这些对象。
@RestController
@RequestMapping("/person")
public class PersonController {
@RequestMapping(
value = "/mail",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json"
)
public String getMail(@RequestBody MailID mail) {
System.out.println(mail.getId()+" "+mail.getMail());
return mail.getMail();
}
@RequestMapping(
value = "/phone",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json"
)
public String getPhone(@RequestBody Phone phone) {
System.out.println(phone.getId()+" "+phone.getPhone());
return phone.getPhone();
}
@RequestMapping(
value = "/info",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json"
)
public String mysqlToEs( @RequestBody MailID mail, @RequestBody Phone phone) {
System.out.println(mail.getMail()+" "+phone.getPhone());
return mail.getMail()+" "+phone.getPhone();
}
}
这个命令工作正常。它完全接受MailID对象值。
curl -X POST -d '{"id":1, "mail":"abc@yahoo.com"}' -H "Content-type:application/json" http://localhost:8084/person/mail
它完全接受Phone对象值。
curl -X POST -d '{"id":1, "phone":"3333212"}' -H "Content-type:application/json" http://localhost:8084/person/phone
现在我想在一个POST请求中传递MailID和Phone值。我尝试了以下curl请求。说它没有用。
curl -X POST -d '{"phone":{"id":1, "phone":"3333212"}}' -H "Content-type:application/json" http://localhost:8084/person/info
我已经读过,我们可以通过创建一个字段为MailId
和Phone
的新对象来解决此问题。这是正确的做法吗?还有其他方法,以便我可以在单个函数本身中访问这两个对象参数吗?
由于
答案 0 :(得分:1)
如果请求正文中有多个对象,则必须使用
@Echo Off
If "%PROCESSOR_ARCHITECTURE:~-2%"=="86" (If Defined PROCESSOR_ARCHITEW6432 (
Set "KEY=\Wow6432Node" & Set "MWB=64") Else (Set "MWB=32" & Set "MOB=32"
)) Else Set "MWB=64"
Set "KEY=HKLM\Software%KEY%\Microsoft\Windows\CurrentVersion\Uninstall"
Set "GUID="
For /F "Delims=" %%A In ('Reg Query %KEY% /K /F "*-001B-*0FF1CE}"') Do (
If Not Defined GUID Set "GUID=%%~nxA")
If Not "%GUID:~-1%"=="}" Set "GUID="
If Not Defined GUID (Echo= Unable to find Office Product & GoTo EndIt)
If %GUID:~4,1% Equ 4 Set "IOV=10" Else (If %GUID:~4,1% Equ 2 (Set "IOV=07"
If Not Defined MOB Set "MOB=32") Else (If %GUID:~4,1% Equ 5 (Set "IOV=13"
) Else (If %GUID:~4,1% Equ 6 Set "IOV=16")))
If Not Defined IOV (Echo= Unknown Office version on your %MWB%-bit OS
GoTo EndIt)
If Not Defined MOB Set "MOB=32"
If %GUID:~20,1% Equ 1 Set "MOB=64"
Echo=&Echo= Office 20%IOV% %MOB%-bit Product installed on a %MWB%-bit OS
:EndIt
Timeout 5 1>Nul
注释而不是
@RequestPart
要使其工作,您必须为字段命名(如果它是HTML表单,则很容易,不确定如何在curl中执行此操作)。
要考虑的一件事是REST API的样式不好,因为它是非对称的(要创建一个POST,但要有两个不同的GET方法)。如果您使用两个字段创建一个对象,则它再次对称。
因此,我认为不会在一次调用中做两次创建操作。
答案 1 :(得分:1)
我建议你创建一个包含另外两个的新对象。像这样:
class MyNewObjectRequest{
private MailID mailId;
private Phone phone;
... some getters/setters
}
然后您需要更改控制器以接收持有者。
@RequestMapping(
value = "/info",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json"
)
public String mysqlToEs( @RequestBody MyNewObjectRequest rq) {
System.out.println(rq.getEmailId().getMail()+" "+rq.getPhone().getPhone());
return rq.getEmailId().getMail()+" "+rq.getPhone().getPhone();
}
请求json看起来像这样:
{"emailId" : {"id":1, "mail":"abc@yahoo.com"}, "phone" : {"id":1, "phone":"3333212"}}