-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d85ed2
commit ef4e6bc
Showing
7 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
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
14 changes: 13 additions & 1 deletion
14
...c/main/java/org/springside/examples/showcase/demos/hystrix/service/ServiceController.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 |
---|---|---|
@@ -1,21 +1,33 @@ | ||
package org.springside.examples.showcase.demos.hystrix.service; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springside.examples.showcase.webservice.rest.RestException; | ||
import org.springside.examples.showcase.webservice.rest.UserDTO; | ||
|
||
/** | ||
* 使用Hystrix访问资源的服务。 | ||
*/ | ||
@Controller | ||
public class ServiceController { | ||
private static Logger logger = LoggerFactory.getLogger(ServiceController.class); | ||
|
||
@RequestMapping(value = "/hystrix/service/{id}", method = RequestMethod.GET) | ||
@ResponseBody | ||
public UserDTO getUser(@PathVariable("id") Long id) { | ||
GetUserCommand command = new GetUserCommand(id); | ||
return command.execute(); | ||
|
||
try { | ||
return command.execute(); | ||
} catch (Exception e) { | ||
logger.error(e.getMessage(), e); | ||
throw new RestException(HttpStatus.SERVICE_UNAVAILABLE); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...howcase/src/main/java/org/springside/examples/showcase/webservice/rest/RestException.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,20 @@ | ||
package org.springside.examples.showcase.webservice.rest; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public class RestException extends RuntimeException { | ||
|
||
public HttpStatus status; | ||
|
||
public RestException() { | ||
} | ||
|
||
public RestException(HttpStatus status) { | ||
this.status = status; | ||
} | ||
|
||
public RestException(HttpStatus status, String message) { | ||
super(message); | ||
this.status = status; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../src/main/java/org/springside/examples/showcase/webservice/rest/RestExceptionHandler.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,37 @@ | ||
package org.springside.examples.showcase.webservice.rest; | ||
|
||
import javax.validation.ConstraintViolationException; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
import org.springside.modules.beanvalidator.BeanValidators; | ||
|
||
@ControllerAdvice | ||
public class RestExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(value = { ConstraintViolationException.class }) | ||
public final ResponseEntity<?> handleException(ConstraintViolationException ex, WebRequest request) { | ||
String body = BeanValidators.extractPropertyAndMessage(ex.getConstraintViolations()).toString(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.TEXT_PLAIN); | ||
return handleExceptionInternal(ex, body, headers, HttpStatus.BAD_REQUEST, request); | ||
} | ||
|
||
@ExceptionHandler(value = { RestException.class }) | ||
public final ResponseEntity<?> handleException(RestException ex, WebRequest request) { | ||
String body = ex.getMessage(); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.TEXT_PLAIN); | ||
|
||
HttpStatus status = (ex.status != null) ? ex.status : HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
return handleExceptionInternal(ex, body, headers, status, request); | ||
} | ||
} |
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