pub.codex.apix.provider.RequestHandlersProvider Maven / Gradle / Ivy
package pub.codex.apix.provider;
import com.google.common.base.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import pub.codex.apix.context.RequestHandler;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.FluentIterable.from;
@Component
public class RequestHandlersProvider {
@Autowired
private final List handlerMappings;
@Autowired
public RequestHandlersProvider(List handlerMappings) {
this.handlerMappings = handlerMappings;
}
/**
* 获取restful api映射对象
*
* @return
*/
public List getRequestHandlers() {
return from(handlerMappings).transformAndConcat(toMappingEntries()).transform(toRequestHandler()).toList();
}
private Function>> toMappingEntries() {
return new Function>>() {
@Override
public Iterable> apply(RequestMappingInfoHandlerMapping input) {
return input.getHandlerMethods().entrySet(); // 获取mapping方法
}
};
}
private Function, RequestHandler> toRequestHandler() {
return new Function, RequestHandler>() {
@Override
public RequestHandler apply(Map.Entry input) {
return new RequestHandler(input.getKey(), input.getValue());
}
};
}
}