Skip to content

Commit

Permalink
- 合并3.5.3的代码
Browse files Browse the repository at this point in the history
- 修改advice名称,避免与其他框架冲突
  • Loading branch information
feiniaojin committed Apr 27, 2024
1 parent b849f2c commit 94c7d2a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
37 changes: 20 additions & 17 deletions src/main/java/com/feiniaojin/gracefulresponse/AutoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.feiniaojin.gracefulresponse.defaults.DefaultResponseFactory;
import com.feiniaojin.gracefulresponse.defaults.DefaultResponseStatusFactoryImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
Expand All @@ -25,27 +26,27 @@
public class AutoConfig {

@Bean
@ConditionalOnMissingBean(value = GlobalExceptionAdvice.class)
public GlobalExceptionAdvice globalExceptionAdvice() {
return new GlobalExceptionAdvice();
@ConditionalOnMissingBean(value = GrGlobalExceptionAdvice.class)
public GrGlobalExceptionAdvice grGlobalExceptionAdvice() {
return new GrGlobalExceptionAdvice();
}

@Bean
@ConditionalOnMissingBean(value = ValidationExceptionAdvice.class)
public ValidationExceptionAdvice validationExceptionAdvice() {
return new ValidationExceptionAdvice();
@ConditionalOnMissingBean(value = GrValidationExceptionAdvice.class)
public GrValidationExceptionAdvice grValidationExceptionAdvice() {
return new GrValidationExceptionAdvice();
}

@Bean
@ConditionalOnMissingBean(NotVoidResponseBodyAdvice.class)
public NotVoidResponseBodyAdvice notVoidResponseBodyAdvice() {
return new NotVoidResponseBodyAdvice();
@ConditionalOnMissingBean(GrNotVoidResponseBodyAdvice.class)
public GrNotVoidResponseBodyAdvice grNotVoidResponseBodyAdvice() {
return new GrNotVoidResponseBodyAdvice();
}

@Bean
@ConditionalOnMissingBean(VoidResponseBodyAdvice.class)
public VoidResponseBodyAdvice voidResponseBodyAdvice() {
return new VoidResponseBodyAdvice();
@ConditionalOnMissingBean(GrVoidResponseBodyAdvice.class)
public GrVoidResponseBodyAdvice grVoidResponseBodyAdvice() {
return new GrVoidResponseBodyAdvice();
}

@Bean
Expand Down Expand Up @@ -76,21 +77,23 @@ public Init init() {
}

@Bean
public I18nAdvice i18nAdvice() {
return new I18nAdvice();
@ConditionalOnProperty(prefix = "graceful-response", name = "i18n", havingValue = "true")
public GrI18nAdvice grI18nAdvice() {
return new GrI18nAdvice();
}

/**
* 国际化配置
* 国际化配置
*
* @return
*/
@Bean
public MessageSource messageSource() {
@ConditionalOnProperty(prefix = "graceful-response", name = "i18n", havingValue = "true")
public MessageSource grMessageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("i18n/graceful-response");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(30);
return messageSource;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
*/
@ControllerAdvice
@Order(200)
public class GlobalExceptionAdvice implements ApplicationContextAware {
public class GrGlobalExceptionAdvice implements ApplicationContextAware {

private final Logger logger = LoggerFactory.getLogger(GlobalExceptionAdvice.class);
private final Logger logger = LoggerFactory.getLogger(GrGlobalExceptionAdvice.class);

@Resource
private ResponseStatusFactory responseStatusFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
*/
@ControllerAdvice
@Order(2000)
public class I18nAdvice implements ResponseBodyAdvice<Response> {
public class GrI18nAdvice implements ResponseBodyAdvice<Response> {

private static final String[] emptyArray = new String[0];

@Resource
private GracefulResponseProperties properties;

@Resource
private MessageSource messageSource;
private MessageSource grMessageSource;

@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
Expand All @@ -52,7 +52,7 @@ public Response beforeBodyWrite(Response body, MethodParameter returnType, Media
}
ResponseStatus bodyStatus = body.getStatus();
String code = bodyStatus.getCode();
String renderMsg = messageSource.getMessage(code, emptyArray, null, locale);
String renderMsg = grMessageSource.getMessage(code, emptyArray, null, locale);
//有国际化配置的才会替换,否则使用默认配置的
if (StringUtils.hasText(renderMsg)) {
bodyStatus.setMsg(renderMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
*/
@ControllerAdvice
@Order(value = 1000)
public class NotVoidResponseBodyAdvice implements ResponseBodyAdvice<Object> {
public class GrNotVoidResponseBodyAdvice implements ResponseBodyAdvice<Object> {

private final Logger logger = LoggerFactory.getLogger(NotVoidResponseBodyAdvice.class);
private final Logger logger = LoggerFactory.getLogger(GrNotVoidResponseBodyAdvice.class);

@Resource
private ResponseFactory responseFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@

@ControllerAdvice
@Order(100)
public class ValidationExceptionAdvice {
public class GrValidationExceptionAdvice {

private final Logger logger = LoggerFactory.getLogger(ValidationExceptionAdvice.class);
private final Logger logger = LoggerFactory.getLogger(GrValidationExceptionAdvice.class);
@Resource
private RequestMappingHandlerMapping requestMappingHandlerMapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
@ControllerAdvice
@Order(value = 1000)
public class VoidResponseBodyAdvice implements ResponseBodyAdvice<Object> {
public class GrVoidResponseBodyAdvice implements ResponseBodyAdvice<Object> {

@Resource
private ResponseFactory responseFactory;
Expand Down

0 comments on commit 94c7d2a

Please sign in to comment.