Skip to content

Instantly share code, notes, and snippets.

@emersonf
Created May 2, 2014 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emersonf/fbbdf63c899830b9d8df to your computer and use it in GitHub Desktop.
Save emersonf/fbbdf63c899830b9d8df to your computer and use it in GitHub Desktop.
A controller that mixes synchronous and asynchronous requests from a single endpoint.
package tv.nativ.mio.publish.common.controller;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.concurrent.Callable;
/**
* A controller that can mixes synchronous and asynchronous responses.
*
* @author emerson
*/
//@Controller
public class MultiModeTestController {
@RequestMapping("/multiModeController")
public Object handleRequest(@RequestParam(required = false, defaultValue = "no") String async) {
if (async.equals("no")) {
return newResponseEntity("sync");
}
return new Callable<ResponseEntity<String>>() {
@Override public ResponseEntity<String> call() throws Exception {
return newResponseEntity("async");
}
};
}
private ResponseEntity<String> newResponseEntity(String body) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<>(body, httpHeaders, HttpStatus.OK);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment