All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.itcoon.common.starter.advice.ResponseBodyHandlerAdvice Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.itcoon.common.starter.advice;

import com.itcoon.common.api.response.ApiResponse;
import com.itcoon.common.i18n.I18ns;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 

* * @author 俞淼霞 * @since 2019-09-06 */ @ControllerAdvice(annotations = {Controller.class, ControllerAdvice.class}) public class ResponseBodyHandlerAdvice implements ResponseBodyAdvice { @Override public boolean supports(MethodParameter methodParameter, Class> aClass) { return true; } @Override public Object beforeBodyWrite(Object payload, MethodParameter methodParameter, MediaType mediaType, Class> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { if (payload != null) { if (payload instanceof ApiResponse) { ApiResponse apiResponse = (ApiResponse) payload; I18ns i18ns = I18ns.getInstance("exception_type"); String message = i18ns.getMessage(String.valueOf(apiResponse.getCode()), apiResponse.getMessage()); apiResponse.setMessage(message); } } return payload; } }