diff --git a/documents/contents/guidebooks/how-to-develop/java/sub-project-settings/message-management.md b/documents/contents/guidebooks/how-to-develop/java/sub-project-settings/message-management.md index aded97b6c..98154a441 100644 --- a/documents/contents/guidebooks/how-to-develop/java/sub-project-settings/message-management.md +++ b/documents/contents/guidebooks/how-to-develop/java/sub-project-settings/message-management.md @@ -73,3 +73,25 @@ public class ErrorMessageBuilder { } } ``` + +また、 `#!java @Service` や `#!java @Controller` 、 `#!java @Component` といった Bean 登録されたクラス内で `MessageSource` を利用する場合は、 `#!java @Autowired` による DI で実装します。 + +以下は、プロパティファイルからエラーレスポンスに含めるメッセージを整形する `ProblemDetailsFactory.java` クラスの例です。 + +```java title="ProblemDetailsFactory.java" hl_lines="4 5 11" +@Component +public class ProblemDetailsFactory { + + @Autowired + private MessageSource messages; + + public ProblemDetail createProblemDetail(ErrorMessageBuilder errorBuilder, String titleId, HttpStatus status) { + + ProblemDetail problemDetail = ProblemDetail.forStatus(status); + + problemDetail.setTitle(messages.getMessage(titleId, new String[] {}, Locale.getDefault())); + + ... + } +} +``` diff --git a/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/constant/ProblemDetailsExtensionConstant.java b/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/constant/WebConstants.java similarity index 66% rename from samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/constant/ProblemDetailsExtensionConstant.java rename to samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/constant/WebConstants.java index 9e6589111..70c4c5f06 100644 --- a/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/constant/ProblemDetailsExtensionConstant.java +++ b/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/constant/WebConstants.java @@ -1,9 +1,9 @@ package com.dressca.web.constant; /** - * ProblemDetailの拡張メンバー用の定数クラス。 + * web プロジェクトで利用する汎用定数クラス。 */ -public class ProblemDetailsExtensionConstant { +public class WebConstants { /** Exception ID。 */ public static final String EXCEPTION_ID = "exceptionId"; diff --git a/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/controller/advice/ProblemDetailsFactory.java b/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/controller/advice/ProblemDetailsFactory.java index 617a36d71..287bc3635 100644 --- a/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/controller/advice/ProblemDetailsFactory.java +++ b/samples/azure-ad-b2c-sample/auth-backend/web/src/main/java/com/dressca/web/controller/advice/ProblemDetailsFactory.java @@ -11,8 +11,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ProblemDetail; import org.springframework.stereotype.Component; -import com.dressca.systemcommon.util.ApplicationContextWrapper; -import com.dressca.web.constant.ProblemDetailsExtensionConstant; +import com.dressca.web.constant.WebConstants; import com.dressca.web.log.ErrorMessageBuilder; /** @@ -24,6 +23,9 @@ public class ProblemDetailsFactory { @Autowired private Environment env; + @Autowired + private MessageSource messages; + /** * エラーレスポンスに含める ProblemDetails を作成する。 * @@ -36,8 +38,7 @@ public ProblemDetail createProblemDetail(ErrorMessageBuilder errorBuilder, Strin ProblemDetail problemDetail = ProblemDetail.forStatus(status); - MessageSource messageSource = (MessageSource) ApplicationContextWrapper.getBean(MessageSource.class); - problemDetail.setTitle(messageSource.getMessage(titleId, new String[] {}, Locale.getDefault())); + problemDetail.setTitle(messages.getMessage(titleId, new String[] {}, Locale.getDefault())); // 開発環境においては、 detail プロパティにスタックトレースを含める // 開発環境かどうかの判断は、環境変数の Profile をもとに判断する @@ -54,8 +55,8 @@ public ProblemDetail createProblemDetail(ErrorMessageBuilder errorBuilder, Strin // 拡張メンバーとして exceptionId と exceptionValues を含める Map errorProperty = new LinkedHashMap() { { - put(ProblemDetailsExtensionConstant.EXCEPTION_ID, errorBuilder.getExceptionId()); - put(ProblemDetailsExtensionConstant.EXCEPTION_VALUES, errorBuilder.getFrontMessageValue()); + put(WebConstants.EXCEPTION_ID, errorBuilder.getExceptionId()); + put(WebConstants.EXCEPTION_VALUES, errorBuilder.getFrontMessageValue()); } }; diff --git a/samples/web-csr/dressca-backend/web/src/main/java/com/dressca/web/constant/WebConstants.java b/samples/web-csr/dressca-backend/web/src/main/java/com/dressca/web/constant/WebConstants.java index 3114e6266..66ad1e3f9 100644 --- a/samples/web-csr/dressca-backend/web/src/main/java/com/dressca/web/constant/WebConstants.java +++ b/samples/web-csr/dressca-backend/web/src/main/java/com/dressca/web/constant/WebConstants.java @@ -1,7 +1,7 @@ package com.dressca.web.constant; /** - * ProblemDetailの拡張メンバー用の定数クラス。 + * web プロジェクトで利用する汎用定数クラス。 */ public class WebConstants {