Last active
September 14, 2015 04:30
-
-
Save arawn/18f14372fb73c45c8268 to your computer and use it in GitHub Desktop.
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
service.info.name=Restful API Service | |
service.info.version=1.0.1 | |
logging.level.org.springframework.boot=DEBUG | |
logging.level.org.springframework.web=DEBUG |
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
package com.sk.planet.web.support; | |
import java.util.Locale; | |
import java.util.Map; | |
import java.util.Objects; | |
import javax.servlet.http.HttpServletRequest; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.autoconfigure.web.ErrorAttributes; | |
import org.springframework.boot.autoconfigure.web.ErrorController; | |
import org.springframework.context.MessageSource; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.util.StringUtils; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.context.request.RequestAttributes; | |
import org.springframework.web.context.request.ServletRequestAttributes; | |
@RestController | |
public class DefaultErrorController implements ErrorController { | |
private String errorPath; | |
private ErrorAttributes errorAttributes; | |
private MessageSource messageSource; | |
@RequestMapping(value = "${error.path:/error}") | |
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request, Locale locale) { | |
RequestAttributes requestAttributes = new ServletRequestAttributes(request); | |
Map<String, Object> body = errorAttributes.getErrorAttributes(requestAttributes, true); | |
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; | |
if(Objects.nonNull(body.get("status"))) { | |
try { | |
status = HttpStatus.valueOf((Integer) body.get("status")); | |
} catch(Exception ignore) { } | |
} | |
String message = messageSource.getMessage("error." + status, null, status.getReasonPhrase(), locale); | |
if(StringUtils.hasText(message)) { | |
body.put("message", message); | |
} | |
return ResponseEntity.status(status).body(body); | |
} | |
@Override | |
public String getErrorPath() { | |
return errorPath; | |
} | |
@Value("${error.path:/error}") | |
public void setErrorPath(String errorPath) { | |
this.errorPath = errorPath; | |
} | |
@Autowired | |
public void setErrorAttributes(ErrorAttributes errorAttributes) { | |
this.errorAttributes = errorAttributes; | |
} | |
@Autowired | |
public void setMessageSource(MessageSource messageSource) { | |
this.messageSource = messageSource; | |
} | |
} |
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
error.404=요청한 자원을 찾을 수 없습니다. | |
error.500=요청을 처리하는 중 서버내에서 알 수 없는 오류가 발생했습니다. |
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
package com.sk.planet.web; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
@ConfigurationProperties("service.info") | |
public class ServiceInfoController { | |
private String name; | |
private String version; | |
@RequestMapping("/info") | |
public Map<String, String> info() { | |
Map<String, String> model = new HashMap<>(); | |
model.put("name", name); | |
model.put("version", version); | |
return model; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setVersion(String version) { | |
this.version = version; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment