@SpringBootApplication
@EnableEurekaClient
public class Gateway1App {
@Autowired
ObjectMapper objectMapper;
public static void main(String[] args) {
SpringApplication.run(Gateway1App.class,args);
}
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
//上面两段代码加进去,可以解决
@Bean
public GlobalFilter globalFilter(){
return new GlobalFilter() {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
exchange.getResponse().getHeaders().set("content-type","application/json;charset=utf-8");
Map<String,Object>result=new HashMap<>(6);
result.put("code", HttpStatus.OK.value());
result.put("msg","ok");
result.put("data",new Object());
byte[] bytes=null;
try {
bytes = objectMapper.writeValueAsBytes(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
DataBuffer wrap = exchange.getResponse().bufferFactory().wrap(bytes);
return exchange.getResponse().writeWith(Mono.just(wrap));
}
};
}
}
ObjectMapper错误的时候,处理&&gateway自定义过滤器
最新推荐文章于 2024-07-03 21:44:20 发布