Having an odd issue where I am able to send requests to my local spring boot web api via CURL i.e
curl -X POST -H "Content-Type: application/json" -d '{"userName":"bob","textMessage":"newMessage","timeOut":500}' http://localhost:8080/chats
curl -X GET -H "Content-Type: application/json" http://localhost:8080/chats/username/bob
curl -X GET -H "Content-Type: application/json" http://localhost:8080/chats/id/b4f1a2c6-74e7-444f-abc9-a72fc61ec515
Here is the POST method
@PostMapping
@Async
public ResponseEntity<String> postMessage(@RequestBody Messages message ){
if(message.getUserName() == null || message.getTextMessage() == null){
System.out.println("No Username or TextMessage");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
if(message.getTimeOut() !=null && message.getTimeOut() < 0){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
String id = UUID.randomUUID().toString();
message.setTextId(id);
if(message.getTimeOut() == null) {
message.setTimeOut(60);
message.setTimeOutDate(60*1000 System.currentTimeMillis());
}
else{
message.setTimeOutDate(message.getTimeOut()*1000 System.currentTimeMillis());
}
System.out.println("Attempting save");
messagesRepository.save(message);
return new ResponseEntity<>("id: " id, HttpStatus.CREATED);
}
I am not getting any output of any kind.
Please advise.
Edit: Image of curl command not returning anything for the post, but in backend we see it working
Edit 2: Ive printed out the reponseentity and it has the correct content, so the error is the return not actually outputting
CodePudding user response:
It is because you have @Async
annotation on your controller, which is releasing the container thread immediately and processing your task asynchronously in another thread.
@Async
can be used for fire-and-forget scenarios, such as sending an email, kicking off a database job, some long-running background task. Caller immediately gets the response, while a background job continues processing.