我不确定1,2,3
.
如果你坚持,
private Response get(List<MyObject> params) {
}
@GET
@Path("/{params}")
public Response get(@PathParam("params") String params) {
return get(Stream.of(params.split(","))
.map(MyObject::new)
.collect(Collectors.toList()));
}
如果你真的坚持,
注解,
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface MyObjectsParam {
}
转换器,
public class MyObjectsParamDateConverter
implements ParamConverter<List<MyObject>> {
@Override
public List<MyObject> fromString(final String value) {
return Stream.of(params.split(","))
.map(MyObject::new)
.collect(Collectors.toList())
}
@Override
public String toString(final List<MyObject> value) {
return value.stream()
.map(o -> o.getValue())
.map(Object::toString)
.collect(Collectors.joining(","));
}
}
提供者,
@Provider
public class MyObjectsParamConverterProvider
implements ParamConverterProvider {
@Override
@SuppressWarnings("unchecked")
default ParamConverter getConverter(final Class<S> rawType,
final Type genericType,
final Annotation[] annotations) {
for (final Annotation annotation : annotations) {
if (MyObjectsParam.class.isInstance(annotation)) {
return new MyObjectsParamDateConverter();
}
}
return null;
}
}
现在你可以像这样使用它了。
@GET
@Path("/{params}") // still '1,2,3'
public Response get(
@PathParam("params")
@MyObjectsParam // IN ACTION!!!
List<MyObject> params) {
}