forked from spring-attic/spring-mvc-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Async" tab and CallableController and DeferredResultController. Add links for mapping requests by content type via URL extension. Add global @ExceptionHandler example.
- Loading branch information
1 parent
ca7ea7f
commit 9c13d23
Showing
17 changed files
with
442 additions
and
40 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/main/java/org/springframework/samples/mvc/async/CallableController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.springframework.samples.mvc.async; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.context.request.async.AsyncTask; | ||
|
||
@Controller | ||
@RequestMapping("/async/callable") | ||
public class CallableController { | ||
|
||
|
||
@RequestMapping("/response-body") | ||
public @ResponseBody Callable<String> callable() { | ||
|
||
return new Callable<String>() { | ||
@Override | ||
public String call() throws Exception { | ||
Thread.sleep(2000); | ||
return "Callable result"; | ||
} | ||
}; | ||
} | ||
|
||
@RequestMapping("/view") | ||
public Callable<String> callableWithView(final Model model) { | ||
|
||
return new Callable<String>() { | ||
@Override | ||
public String call() throws Exception { | ||
Thread.sleep(2000); | ||
model.addAttribute("foo", "bar"); | ||
model.addAttribute("fruit", "apple"); | ||
return "views/html"; | ||
} | ||
}; | ||
} | ||
|
||
@RequestMapping("/exception") | ||
public @ResponseBody Callable<String> callableWithException( | ||
final @RequestParam(required=false, defaultValue="true") boolean handled) { | ||
|
||
return new Callable<String>() { | ||
@Override | ||
public String call() throws Exception { | ||
Thread.sleep(2000); | ||
if (handled) { | ||
// see handleException method further below | ||
throw new IllegalStateException("Callable error"); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Callable error"); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@RequestMapping("/custom-timeout") | ||
public @ResponseBody AsyncTask<String> callableWithCustomTimeout() { | ||
|
||
Callable<String> callable = new Callable<String>() { | ||
@Override | ||
public String call() throws Exception { | ||
Thread.sleep(2000); | ||
return "Callable result"; | ||
} | ||
}; | ||
|
||
return new AsyncTask<String>(1000, callable); | ||
} | ||
|
||
@ExceptionHandler | ||
@ResponseBody | ||
public String handleException(IllegalStateException ex) { | ||
return "Handled exception: " + ex.getMessage(); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/org/springframework/samples/mvc/async/DeferredResultController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.springframework.samples.mvc.async; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.PriorityBlockingQueue; | ||
|
||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.context.request.async.DeferredResult; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
@Controller | ||
@RequestMapping("/async") | ||
public class DeferredResultController { | ||
|
||
private final Queue<DeferredResult<String>> responseBodyQueue = new PriorityBlockingQueue<DeferredResult<String>>(); | ||
|
||
private final Queue<DeferredResult<ModelAndView>> mavQueue = new PriorityBlockingQueue<DeferredResult<ModelAndView>>(); | ||
|
||
private final Queue<DeferredResult<String>> exceptionQueue = new PriorityBlockingQueue<DeferredResult<String>>(); | ||
|
||
|
||
@RequestMapping("/deferred-result/response-body") | ||
public @ResponseBody DeferredResult<String> deferredResult() { | ||
DeferredResult<String> result = new DeferredResult<String>(); | ||
this.responseBodyQueue.add(result); | ||
return result; | ||
} | ||
|
||
@RequestMapping("/deferred-result/model-and-view") | ||
public @ResponseBody DeferredResult<ModelAndView> deferredResultWithView() { | ||
DeferredResult<ModelAndView> result = new DeferredResult<ModelAndView>(); | ||
this.mavQueue.add(result); | ||
return result; | ||
} | ||
|
||
@RequestMapping("/deferred-result/exception") | ||
public @ResponseBody DeferredResult<String> deferredResultWithException() { | ||
DeferredResult<String> result = new DeferredResult<String>(); | ||
this.exceptionQueue.add(result); | ||
return result; | ||
} | ||
|
||
@RequestMapping("/deferred-result/timeout-value") | ||
public @ResponseBody DeferredResult<String> deferredResultWithTimeoutValue() { | ||
|
||
// Provide a default result in case of timeout and override the timeout value | ||
// set in src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml | ||
|
||
return new DeferredResult<String>(1000L, "Deferred result after timeout"); | ||
} | ||
|
||
@Scheduled(fixedRate=2000) | ||
public void processQueues() { | ||
for (DeferredResult<String> result : this.responseBodyQueue) { | ||
result.setResult("Deferred result"); | ||
this.responseBodyQueue.remove(result); | ||
} | ||
for (DeferredResult<String> result : this.exceptionQueue) { | ||
result.setErrorResult(new IllegalStateException("DeferredResult error")); | ||
this.exceptionQueue.remove(result); | ||
} | ||
for (DeferredResult<ModelAndView> result : this.mavQueue) { | ||
result.setResult(new ModelAndView("views/html", "javaBean", new JavaBean("bar", "apple"))); | ||
this.mavQueue.remove(result); | ||
} | ||
} | ||
|
||
@ExceptionHandler | ||
@ResponseBody | ||
public String handleException(IllegalStateException ex) { | ||
return "Handled exception: " + ex.getMessage(); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/springframework/samples/mvc/async/JavaBean.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.springframework.samples.mvc.async; | ||
|
||
public class JavaBean { | ||
|
||
private String foo; | ||
|
||
private String fruit; | ||
|
||
public JavaBean(String foo, String fruit) { | ||
this.foo = foo; | ||
this.fruit = fruit; | ||
} | ||
|
||
public String getFoo() { | ||
return foo; | ||
} | ||
|
||
public void setFoo(String foo) { | ||
this.foo = foo; | ||
} | ||
|
||
public String getFruit() { | ||
return fruit; | ||
} | ||
|
||
public void setFruit(String fruit) { | ||
this.fruit = fruit; | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
6 changes: 6 additions & 0 deletions
6
src/main/java/org/springframework/samples/mvc/exceptions/BusinessException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.springframework.samples.mvc.exceptions; | ||
|
||
@SuppressWarnings("serial") | ||
public class BusinessException extends Exception { | ||
|
||
} |
This file contains 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
15 changes: 15 additions & 0 deletions
15
src/main/java/org/springframework/samples/mvc/exceptions/GlobalExceptionHandler.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.springframework.samples.mvc.exceptions; | ||
|
||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler | ||
public @ResponseBody String handleBusinessException(BusinessException ex) { | ||
return "Handled BusinessException"; | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.