Created
May 2, 2014 15:20
-
-
Save emersonf/fbbdf63c899830b9d8df to your computer and use it in GitHub Desktop.
A controller that mixes synchronous and asynchronous requests from a single endpoint.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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