Apache ShenYu
gateway.org.apache.shenyu.plugin.api.result.DefaultShenyuResult
.public class ShenyuDefaultEntity implements Serializable { private static final long serialVersionUID = -2792556188993845048L; private Integer code; private String message; private Object data; }
json
as follows:{ "code": -100, //response code, "message": "Your parameter error, please check the relevant documentation!", //hint messages "data": null // business data }
CustomShenyuResult
and implements org.apache.shenyu.plugin.api.result.ShenyuResult
/** * The interface shenyu result. */ public interface ShenyuResult<T> { /** * The response result. * * @param exchange the exchange * @param formatted the formatted object * @return the result object */ default Object result(ServerWebExchange exchange, Object formatted) { return formatted; } /** * format the origin, default is json format. * * @param exchange the exchange * @param origin the origin * @return format origin */ default Object format(ServerWebExchange exchange, Object origin) { // basic data if (ObjectTypeUtils.isBasicType(origin)) { return origin; } // error result or rpc origin result. return JsonUtils.toJson(origin); } /** * the response context type, default is application/json. * * @param exchange the exchange * @param formatted the formatted data that is origin data or byte[] convert string * @return the context type */ default MediaType contentType(ServerWebExchange exchange, Object formatted) { return MediaType.APPLICATION_JSON; } /** * Error t. * * @param code the code * @param message the message * @param object the object * @return the t */ T error(int code, String message, Object object); }
Processing sequence:
format
->contextType
->result
. Theformat
method performs data formatting. If the data is a basic type and returns itself, other types are converted toJSON
, and the parameterorigin
is the original data. Formatting can be performed according to the situation.contextType
, if it is a basic type, usetext/plain
, the default isapplication/json
, the parameterformatted
is the data processed by theformat
method, and can be combined with the return result offormat
for data type Define processing. The parameterformatted
ofresult
is the data processed by theformat
method, which returns to itself by default, and can be combined with the return result offormat
for custom processing of the data type.
T
is a generic parameter for your response data.Spring Bean
.@Bean public ShenyuResult<?> customShenyuResult() { return new CustomShenyuResult(); }