gpt4 book ai didi

java - 发布并接收结果

转载 作者:行者123 更新时间:2023-12-02 10:38:07 27 4
gpt4 key购买 nike

安卓空间

void post(Food food)
{
Gson gson = new Gson();
String jsonFood = gson.toJson(food);

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

restTemplate.postForEntity(URL, jsonFood, String.class);
}

后端空间

@PostMapping("/food")
public void postFood(@RequestBody String foodJson)
{
Food food = new GsonBuilder().create().fromJson(foodJson, Food.class);

String id = createId(food);
// now how do I send back saying I got this and here is an id?

}

收到后我想回复说我收到了信息并发回了 ID。

最佳答案

Spring boot 会使用 Jackson 自动将 json 转换为模型对象

@PostMapping("/food")
public YourResponse postFood(@RequestBody Food food)
{
String id = createId(food);
return new YourResponse(id,"hello World");
}

响应对象

 public class YourResponse{
private String id;
private String response;
//.. constructor, getter setter
}

关于java - 发布并接收结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53126668/

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