When using Netbeans and writing an arbitrary REST endpoint, NetBeans always displays a warning that the method can be converted to asynchronous.
For example, I create the following method:
@GET
@Path("/test")
public String hello() {
return "Hello World!";
}
NetBeans then shows a warning, see below:
Clicking on the tooltip generates this code:
private final ExecutorService executorService = java.util.concurrent.Executors.newCachedThreadPool();
@GET
@Path(value = "/test")
public void hello(@Suspended final AsyncResponse asyncResponse) {
executorService.submit(new Runnable() {
@Override
public void run() {
asyncResponse.resume(doHello());
}
});
}
private String doHello() {
return "Hello World!";
}
The same holds true when creating a PUT or POST method. Since NetBeans always shows a warning when a REST endpoint is implemented, this tells me that writing synchronous endpoints is considered wrong/bad practice. So, should every REST endpoint be asynchronous? Why?