73|RESTful Web Services(37):再增加converter方法时,步骤有什么不同?
徐昊
你好,我是徐昊。今天我们继续使用 TDD 的方式实现 RESTful Web Services。
回顾架构愿景与任务列表
目前的任务列表:
Resource/RootResource/ResourceMethods
当 HEAD 方法映射到 GET 方法时,忽略 GET 的返回值
当没有 OPTIONS 方法时,提供默认实现
代码为:
package geektime.tdd.rest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.container.ResourceContext;
import jakarta.ws.rs.core.GenericEntity;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static geektime.tdd.rest.DefaultResourceMethod.ValueConverter.singeValued;
import static java.util.Arrays.stream;
interface ResourceRouter {
OutboundResponse dispatch(HttpServletRequest request, ResourceContext resourceContext);
interface Resource extends UriHandler {
Optional<ResourceMethod> match(UriTemplate.MatchResult result, String httpMethod, String[] mediaTypes, ResourceContext resourceContext, UriInfoBuilder builder);
}
interface ResourceMethod extends UriHandler {
String getHttpMethod();
GenericEntity<?> call(ResourceContext resourceContext, UriInfoBuilder builder);
}
}
class DefaultResourceRouter implements ResourceRouter {
private Runtime runtime;
private List<Resource> rootResources;
public DefaultResourceRouter(Runtime runtime, List<Resource> rootResources) {
this.runtime = runtime;
this.rootResources = rootResources;
}
@Override
public OutboundResponse dispatch(HttpServletRequest request, ResourceContext resourceContext) {
String path = request.getServletPath();
UriInfoBuilder uri = runtime.createUriInfoBuilder(request);
Optional<ResourceMethod> method = UriHandlers.mapMatched(path, rootResources, (result, resource) -> findResourceMethod(request, resourceContext, uri, result, resource));
if (method.isEmpty()) return (OutboundResponse) Response.status(Response.Status.NOT_FOUND).build();
return (OutboundResponse) method.map(m -> m.call(resourceContext, uri))
.map(entity -> (entity.getEntity() instanceof OutboundResponse) ? (OutboundResponse) entity.getEntity() : Response.ok(entity).build())
.orElseGet(() -> Response.noContent().build());
}
private Optional<ResourceMethod> findResourceMethod(HttpServletRequest request, ResourceContext resourceContext, UriInfoBuilder uri, Optional<UriTemplate.MatchResult> matched, Resource handler) {
return handler.match(matched.get(), request.getMethod(),
Collections.list(request.getHeaders(HttpHeaders.ACCEPT)).toArray(String[]::new), resourceContext, uri);
}
}
class DefaultResourceMethod implements ResourceRouter.ResourceMethod {
private String httpMethod;
private UriTemplate uriTemplate;
private Method method;
public DefaultResourceMethod(Method method) {
this.method = method;
this.uriTemplate = new PathTemplate(Optional.ofNullable(method.getAnnotation(Path.class)).map(Path::value).orElse(""));
this.httpMethod = stream(method.getAnnotations()).filter(a -> a.annotationType().isAnnotationPresent(HttpMethod.class))
.findFirst().get().annotationType().getAnnotation(HttpMethod.class).value();
}
@Override
public String getHttpMethod() {
return httpMethod;
}
@Override
public UriTemplate getUriTemplate() {
return uriTemplate;
}
@Override
public GenericEntity<?> call(ResourceContext resourceContext, UriInfoBuilder builder) {
try {
UriInfo uriInfo = builder.createUriInfo();
Object result = method.invoke(builder.getLastMatchedResource(),
stream(method.getParameters()).map(parameter ->
providers.stream().map(provider -> provider.provide(parameter, uriInfo)).filter(Optional::isPresent)
.findFirst()
.flatMap(values -> values.flatMap(v -> convert(parameter, v)))
.orElse(null)).toArray(Object[]::new));
return result != null ? new GenericEntity<>(result, method.getGenericReturnType()) : null;
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private Optional<Object> convert(Parameter parameter, List<String> values) {
return PrimitiveConverter.convert(parameter, values)
.or(() -> ConverterConstructor.convert(parameter.getType(), values.get(0)));
}
private static ValueProvider pathParam = (parameter, uriInfo) ->
Optional.ofNullable(parameter.getAnnotation(PathParam.class))
.map(annotation -> uriInfo.getPathParameters().get(annotation.value()));
private static ValueProvider queryParam = (parameter, uriInfo) ->
Optional.ofNullable(parameter.getAnnotation(QueryParam.class))
.map(annotation -> uriInfo.getQueryParameters().get(annotation.value()));
private static List<ValueProvider> providers = List.of(pathParam, queryParam);
interface ValueProvider {
Optional<List<String>> provide(Parameter parameter, UriInfo uriInfo);
}
interface ValueConverter<T> {
T fromString(List<String> values);
static <T> ValueConverter<T> singeValued(Function<String, T> converter) {
return values -> converter.apply(values.get(0));
}
}
@Override
public String toString() {
return method.getDeclaringClass().getSimpleName() + "." + method.getName();
}
}
class PrimitiveConverter {
private static Map<Type, DefaultResourceMethod.ValueConverter<Object>> primitives = Map.of(
int.class, singeValued(Integer::parseInt),
double.class, singeValued(Double::parseDouble),
short.class, singeValued(Short::parseShort),
float.class, singeValued(Float::parseFloat),
byte.class, singeValued(Byte::parseByte),
boolean.class, singeValued(Boolean::parseBoolean),
String.class, singeValued(s -> s));
public static Optional<Object> convert(Parameter parameter, List<String> values) {
return Optional.ofNullable(primitives.get(parameter.getType()))
.map(c -> c.fromString(values));
}
}
class ConverterConstructor {
public static Optional<Object> convert(Class<?> converter, String value) {
try {
return Optional.of(converter.getConstructor(String.class).newInstance(value));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
return Optional.empty();
}
}
}
class ResourceMethods {
private Map<String, List<ResourceRouter.ResourceMethod>> resourceMethods;
public ResourceMethods(Method[] methods) {
this.resourceMethods = getResourceMethods(methods);
}
private static Map<String, List<ResourceRouter.ResourceMethod>> getResourceMethods(Method[] methods) {
return stream(methods).filter(m -> stream(m.getAnnotations())
.anyMatch(a -> a.annotationType().isAnnotationPresent(HttpMethod.class)))
.map(DefaultResourceMethod::new)
.collect(Collectors.groupingBy(ResourceRouter.ResourceMethod::getHttpMethod));
}
public Optional<ResourceRouter.ResourceMethod> findResourceMethods(String path, String method) {
return findMethod(path, method).or(() -> findAlternative(path, method));
}
private Optional<ResourceRouter.ResourceMethod> findAlternative(String path, String method) {
if (HttpMethod.HEAD.equals(method)) return findMethod(path, HttpMethod.GET).map(HeadResourceMethod::new);
if (HttpMethod.OPTIONS.equals(method)) return Optional.of(new OptionResourceMethod(path));
return Optional.empty();
}
private Optional<ResourceRouter.ResourceMethod> findMethod(String path, String method) {
return Optional.ofNullable(resourceMethods.get(method)).flatMap(methods -> UriHandlers.match(path, methods, r -> r.getRemaining() == null));
}
class OptionResourceMethod implements ResourceRouter.ResourceMethod {
private String path;
public OptionResourceMethod(String path) {
this.path = path;
}
@Override
public String getHttpMethod() {
return HttpMethod.OPTIONS;
}
@Override
public GenericEntity<?> call(ResourceContext resourceContext, UriInfoBuilder builder) {
return new GenericEntity<>(Response.noContent().allow(findAllowedMethods()).build(), Response.class);
}
private Set<String> findAllowedMethods() {
Set<String> allowed = List.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.PUT,
HttpMethod.POST, HttpMethod.DELETE, HttpMethod.PATCH).stream()
.filter(method -> findMethod(path, method).isPresent()).collect(Collectors.toSet());
allowed.add(HttpMethod.OPTIONS);
if (allowed.contains(HttpMethod.GET)) allowed.add(HttpMethod.HEAD);
return allowed;
}
@Override
public UriTemplate getUriTemplate() {
return new PathTemplate(path);
}
}
}
class HeadResourceMethod implements ResourceRouter.ResourceMethod {
ResourceRouter.ResourceMethod method;
public HeadResourceMethod(ResourceRouter.ResourceMethod method) {
this.method = method;
}
@Override
public String getHttpMethod() {
return HttpMethod.HEAD;
}
@Override
public GenericEntity<?> call(ResourceContext resourceContext, UriInfoBuilder builder) {
method.call(resourceContext, builder);
return null;
}
@Override
public UriTemplate getUriTemplate() {
return method.getUriTemplate();
}
}
class SubResourceLocators {
private final List<ResourceRouter.Resource> subResourceLocators;
public SubResourceLocators(Method[] methods) {
subResourceLocators = stream(methods).filter(m -> m.isAnnotationPresent(Path.class) &&
stream(m.getAnnotations()).noneMatch(a -> a.annotationType().isAnnotationPresent(HttpMethod.class)))
.map((Function<Method, ResourceRouter.Resource>) SubResourceLocator::new).toList();
}
public Optional<ResourceRouter.ResourceMethod> findSubResourceMethods(String path, String method, String[] mediaTypes, ResourceContext resourceContext, UriInfoBuilder builder) {
return UriHandlers.mapMatched(path, subResourceLocators, (result, locator) -> locator.match(result.get(), method, mediaTypes, resourceContext, builder));
}
static class SubResourceLocator implements ResourceRouter.Resource {
private PathTemplate uriTemplate;
private Method method;
public SubResourceLocator(Method method) {
this.method = method;
this.uriTemplate = new PathTemplate(method.getAnnotation(Path.class).value());
}
@Override
public UriTemplate getUriTemplate() {
return uriTemplate;
}
@Override
public String toString() {
return method.getDeclaringClass().getSimpleName() + "." + method.getName();
}
@Override
public Optional<ResourceRouter.ResourceMethod> match(UriTemplate.MatchResult result, String httpMethod, String[] mediaTypes, ResourceContext resourceContext, UriInfoBuilder builder) {
Object resource = builder.getLastMatchedResource();
try {
Object subResource = method.invoke(resource);
return new ResourceHandler(subResource, uriTemplate).match(result, httpMethod, mediaTypes, resourceContext, builder);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
class ResourceHandler implements ResourceRouter.Resource {
private UriTemplate uriTemplate;
private ResourceMethods resourceMethods;
private SubResourceLocators subResourceLocators;
private Function<ResourceContext, Object> resource;
public ResourceHandler(Class<?> resourceClass) {
this(resourceClass, new PathTemplate(getTemplate(resourceClass)), rc -> rc.getResource(resourceClass));
}
private static String getTemplate(Class<?> resourceClass) {
if (!resourceClass.isAnnotationPresent(Path.class)) throw new IllegalArgumentException();
return resourceClass.getAnnotation(Path.class).value();
}
public ResourceHandler(Object resource, UriTemplate uriTemplate) {
this(resource.getClass(), uriTemplate, rc -> resource);
}
private ResourceHandler(Class<?> resourceClass, UriTemplate uriTemplate, Function<ResourceContext, Object> resource) {
this.uriTemplate = uriTemplate;
this.resourceMethods = new ResourceMethods(resourceClass.getMethods());
this.subResourceLocators = new SubResourceLocators(resourceClass.getMethods());
this.resource = resource;
}
@Override
public Optional<ResourceRouter.ResourceMethod> match(UriTemplate.MatchResult result, String httpMethod, String[] mediaTypes, ResourceContext resourceContext, UriInfoBuilder builder) {
builder.addMatchedResource(resource.apply(resourceContext));
String remaining = Optional.ofNullable(result.getRemaining()).orElse("");
return resourceMethods.findResourceMethods(remaining, httpMethod)
.or(() -> subResourceLocators.findSubResourceMethods(remaining, httpMethod, mediaTypes, resourceContext, builder));
}
@Override
public UriTemplate getUriTemplate() {
return uriTemplate;
}
}
视频演示
进入今天的环节:
00:00 / 00:00
1.0x
- 2.0x
- 1.5x
- 1.25x
- 1.0x
- 0.75x
- 0.5x
音量
网页全屏
全屏
00:00
思考题
思考:如何提取方法调用部分,交给 sub resource 使用?
分享:对于学习 TDD,你的初衷是什么?驱动力是什么呢?
欢迎把你的思考和想法分享在评论区,我们下节课再见!
确认放弃笔记?
放弃后所记笔记将不保留。
新功能上线,你的历史笔记已初始化为私密笔记,是否一键批量公开?
批量公开的笔记不会为你同步至部落
公开
同步至部落
取消
完成
0/2000
荧光笔
直线
曲线
笔记
复制
AI
- 深入了解
- 翻译
- 英语
- 中文简体
- 中文繁体
- 法语
- 德语
- 日语
- 韩语
- 俄语
- 西班牙语
- 阿拉伯语
- 解释
- 总结
本文介绍了使用TDD(测试驱动开发)的方式实现RESTful Web Services的过程。文章首先回顾了当前的任务列表,包括Resource/RootResource/ResourceMethods等。接着详细介绍了DefaultResourceRouter和DefaultResourceMethod的实现,包括dispatch方法的实现和ResourceMethod的调用过程。同时,还介绍了PrimitiveConverter和ConverterConstructor的作用,以及ResourceMethods和SubResourceLocators的实现。最后,提出了两个思考题,分别是如何提取方法调用部分交给sub resource使用,以及对于学习TDD的初衷和驱动力。整篇文章通过代码和注释详细解释了RESTful Web Services的实现过程,对于想要了解TDD和RESTful Web Services实现的读者具有很高的参考价值。
仅可试看部分内容,如需阅读全部内容,请付费购买文章所属专栏
《徐昊 · TDD 项目实战 70 讲》,新⼈⾸单¥98
《徐昊 · TDD 项目实战 70 讲》,新⼈⾸单¥98
立即购买
登录 后留言
全部留言(2)
- 最新
- 精选
- java小霸王初衷就是为了让自己更专业吧,提高效率2024-03-05归属地:广东
- aoe学习 TDD 的初衷:不要因为重大 Bug 丢掉工作 驱动力: 重要功能可以靠测试提高正确率,而不是靠运气; 可以即时重构,代码越来越好(更容易修改); 使用测试代替 Debug、查看日志调试,提升了工作效率; 使用动态测试轻松应对复杂场景; 最近发生的两个 Bug 终于不是服务端代码导致的(一个是新服务发布后有一个旧的实例没有下线,另一个是客户端组装请求参数格式有误); 虽然学习 TDD 阶段开发的时间加长了,但功能完成后基本可以顺利通过测试部门验证,不用再回头找 Bug、改 Bug 了,总体工作时间缩短了2022-09-03归属地:浙江
收起评论