gpt4 book ai didi

java - 我如何在 angular2 post 请求中发送 2 个对象

转载 作者:行者123 更新时间:2023-12-01 16:51:17 25 4
gpt4 key购买 nike

我有这个代码:

客户端(Angular2)

     saveConfig(configType: ConfigTypes, gasConfigModel: GasConfigModel): any {
console.info("sending post request");
let headers = new Headers({
'Content-Type': 'application/json'
});

....

return this.http
.post(this.url, formParamString, ??, {headers: headers})
.map(res => res.json())
.subscribe(
data => {
console.info("next: ");
console.info(data)
},
err => console.error(err)
);
}

服务器端:

@Path("/SaveConfig")
@POST
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public void saveConfig(MyObj my object, CountryGasStationConfig countryGasStationConfig) throws Exception {....}

我知道如何在 post 中发送 1 个对象,但如何在 post 请求中发送 2 个对象?

最佳答案

由于您使用 Json 作为媒体,因此在技术上不可能实现这一点,因为单个文档中的两个单独的任意对象将是无效的 Json。

但是,这里的一个潜在解决方案是创建一个包装这两个对象的对象。

请求对象

public SaveConfigRequest {
private MyObj myObject;
private CountryGasStationConfig countryGasStationConfig;


//getters and setters
}

新方法签名

public void saveConfig(SaveConfigRequest request)

示例 Json 文档

{
"myObject": {...},
"countryGasStationConfig": {...}
}

关于java - 我如何在 angular2 post 请求中发送 2 个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39641434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com