public class RequestBodyRoutePredicateFactory extends AbstractRoutePredicateFactory<RequestBodyRoutePredicateFactory.Config> { protected static final Log LOGGER = LogFactory.getLog(RequestBodyRoutePredicateFactory.class); private final List<HttpMessageReader<?>> messageReaders; public RequestBodyRoutePredicateFactory() { super(RequestBodyRoutePredicateFactory.Config.class); this.messageReaders = HandlerStrategies.withDefaults().messageReaders(); } public RequestBodyRoutePredicateFactory(List<HttpMessageReader<?>> messageReaders) { super(RequestBodyRoutePredicateFactory.Config.class); this.messageReaders = messageReaders; } public static final String REQUEST_BODY_ATTR = "requestBodyAttr"; @Override public AsyncPredicate<ServerWebExchange> applyAsync(Config config) { return exchange -> { if (!"POST".equals(exchange.getRequest().getMethodValue())&&!"PUT".equals(exchange.getRequest().getMethodValue())) { return Mono.just(true); } Object cachedBody = exchange.getAttribute(REQUEST_BODY_ATTR); if (cachedBody != null) { try { return Mono.just(true); } catch (ClassCastException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Predicate test failed because class in predicate does not match the cached body object", e); } } return Mono.just(true); } else { return ServerWebExchangeUtils.cacheRequestBodyAndRequest(exchange, (serverHttpRequest) -> ServerRequest.create(exchange.mutate().request(serverHttpRequest).build(), this.messageReaders).bodyToMono(String.class).defaultIfEmpty("").doOnNext((objectValue) -> { if(StringUtils.isBlank(objectValue)){ exchange.getAttributes().put(REQUEST_BODY_ATTR, JSON.toJSONString(exchange.getRequest().getQueryParams())); }else { exchange.getAttributes().put(REQUEST_BODY_ATTR, objectValue); } }).map((objectValue) -> true)); } };
时间: 2024-02-14 20:32:56 浏览: 42
这段代码是一个 Spring Cloud Gateway 的请求体谓词工厂类,用于判断请求的 HTTP 方法是否为 POST 或 PUT,并处理请求的 body 数据。具体可以分为以下几个部分:
1. 继承了 Spring Cloud Gateway 的 AbstractRoutePredicateFactory 抽象类,并实现了 Config 配置类。
2. 在构造函数中初始化了默认的 HttpMessageReader 集合,用于读取请求体中的数据。
3. applyAsync 方法是该类的核心方法,通过异步的方式判断是否满足条件。
4. 判断 HTTP 方法是否为 POST 或 PUT,如果不是则直接返回 true。
5. 如果请求体已经被缓存,则直接返回 true。
6. 如果请求体还没有被缓存,则通过 ServerWebExchangeUtils.cacheRequestBodyAndRequest 方法缓存请求体,并将其转化为 String 类型。
7. 判断请求体是否为空,如果为空,则将请求参数转化为 JSON 字符串并缓存到 REQUEST_BODY_ATTR 中。
8. 如果请求体不为空,则将其缓存到 REQUEST_BODY_ATTR 中。
9. 最后返回 true 表示请求满足条件。
相关问题
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> 中的MyViewHolder是哪来的
MyViewHolder是一个自定义的内部类,它需要继承自RecyclerView.ViewHolder,用于保存RecyclerView中每个item的控件引用。这个自定义的内部类通常定义在MyAdapter类的内部,例如:
```
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
public static class MyViewHolder extends RecyclerView.ViewHolder {
// 在这里定义该item的控件引用
TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
// 在这里初始化控件引用
textView = itemView.findViewById(R.id.text_view);
}
}
// 在这里定义其他方法,例如onCreateViewHolder, onBindViewHolder等等
}
```
在这个例子中,MyAdapter的内部类MyViewHolder继承了RecyclerView.ViewHolder,用于保存每个item中的TextView控件引用。同时,在MyViewHolder的构造函数中对TextView进行初始化,以便在后续数据更新时能够及时更新TextView的内容。
package hadoop9999.training.exp1; import com.alibaba.fastjson.JSON; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class ActorBestMoviesMapper extends Mapper<LongWritable, Text, MovieInfo, NullWritable>{ public static final Log log = LogFactory.getLog(ActorBestMoviesMapper.class); @Override protected void map(LongWritable key1, Text value1, Context context) throws IOException, InterruptedException { String val=value1.toString(); MovieInfo m=JSON.parseObject(val, MovieInfo.class); if(m.getActorSet().contains("李治廷")){ log.info(m.getTitle()); context.write(m, NullWritable.get()); } } }
这是一个Hadoop MapReduce程序中的Mapper类,用于实现从输入数据中筛选出演员“李治廷”参演的电影,并将其输出。具体来说,它接受的输入键值对类型为LongWritable和Text,表示输入文件中每行的偏移量和内容。在map()方法中,它首先将输入数据解析成一个MovieInfo对象,然后检查该电影的演员列表中是否包含“李治廷”,如果包含,则输出该电影对象作为键,NullWritable.get()作为值,以便在Reduce过程中对电影进行排序和去重。同时,它还使用了Log4j2日志框架记录每个被筛选出的电影标题,以便进行调试和监控。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)