i have created RequestBody
with Gson and add to Post
request, but in the controller
of api (url), all attrs return null.
Add to RequestBody
:
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject();
jsonObject.put("dataFromAppChinh", "demo thoi");
jsonObject.put("path", path);
String body = gson.toJson(jsonObject);
RequestBody requestBody = RequestBody.create(body, JSON);
Request request = new Request.Builder().url("http://" + path + "/api/startapp").post(requestBody).build();
Controller
@RestController
@RequestMapping("/api")
public class ChatBot {
@PostMapping(value = "/startapp")
@ResponseBody
public Object start(@RequestBody() ChatBotResponse item) {
try {
item.setResponseList(startApp(item.getPath()));
return item;
} catch (Exception ex) {
log.error(ex);
return ex.getMessage();
}
}
}
My ChatBotResponse
POJO:
@Data
public class ChatBotResponse
{
private String dataFromAppChinh;
private String responseList;
private String path;
}
i found problem is gson.toJson(jsonObject)
insert map
string before my Json String
.
I changed to jsonObject.toString()
and it worked.