我给了一个ParamConverter提供Instant(日期)的字符串,该字符串的格式为Instant的本机ISO-8601,或者为自该纪元以来的整数毫秒。这工作正常,但我还需要能够支持其他日期格式(客户比较挑剔)。
ParamConverter
Instant
为避免传统dd/mm/yyyy与mm/dd/yyyy歧义,我希望客户在请求中指定其首选格式*。例如:
dd/mm/yyyy
mm/dd/yyyy
GET http://api.example.com/filter?since=01/02/2000&dateformat=dd/mm/yyyy
传递给看起来像这样的方法:
@GET String getFilteredList( final @QueryParam( "since" ) Instant since ) { ... }
(为清楚起见,省略了时区和时区部分)
因此,我希望我ParamConverter<Instant>能够读取该dateformat参数。
ParamConverter<Instant>
dateformat
我已经能够使用设置ContainerRequestContext属性和进行AbstractValueFactoryProvider类似操作的过滤器的组合,但是需要该参数应用自定义注释,并且不允许它与QueryParam/ FormParam / etc一起使用,它远没有用。
ContainerRequestContext
AbstractValueFactoryProvider
有什么方法可以从ParamConverter内部获取其他参数或请求对象本身吗?
[*]在现实世界中,这将来自多种预先批准的格式,但是现在仅假设它们正在将输入的内容提供给 DateTimeFormatter
DateTimeFormatter
为了清楚起见,这是我的代码:
public class InstantParameterProvider implements ParamConverterProvider { private static final ParamConverter<Instant> INSTANT_CONVERTER = new ParamConverter<Instant>( ) { @Override public final T fromString( final String value ) { // This is where I would like to get the other parameter's value // Is it possible? } @Override public final String toString( final T value ) { return value.toString( ); } }; @SuppressWarnings( "unchecked" ) @Override public <T> ParamConverter<T> getConverter( final Class<T> rawType, final Type genericType, final Annotation[] annotations ) { if( rawType == Instant.class ) { return (ParamConverter<T>) INSTANT_CONVERTER; } return null; } }
如前所述这里的关键,这是注射一些上下文对象javax.inject.Provider,这使我们能够懒洋洋地检索对象。由于ParamConverterProvider是由Jersey管理的组件,因此我们应该能够注入其他组件。
javax.inject.Provider
ParamConverterProvider
问题在于我们需要的组件将位于请求范围内。为了解决这个问题,我们注入javax.inject.Provider<UriInfo>了提供者。当我们实际调用get()中Provider获得的实际情况UriInfo,这将是在一个请求。对于需要请求范围的任何其他组件也是如此。
javax.inject.Provider<UriInfo>
get()
Provider
UriInfo
例如
public class InstantParamProvider implements ParamConverterProvider { @Inject private javax.inject.Provider<UriInfo> uriInfoProvider; @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (rawType != Instant.class) return null; return new ParamConverter<T>() { @Override public T fromString(String value) { UriInfo uriInfo = uriInfoProvider.get(); String format = uriInfo.getQueryParameters().getFirst("date-format"); if (format == null) { throw new WebApplicationException(Response.status(400) .entity("data-format query parameter required").build()); } else { try { // parse and return here } catch (Exception ex) { throw new WebApplicationException( Response.status(400).entity("Bad format " + format).build()); } } } @Override public String toString(T value) { return value.toString(); } }; } }
这是使用Jersey测试框架的完整示例
import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.logging.Logger; import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import javax.ws.rs.ext.ParamConverter; import javax.ws.rs.ext.ParamConverterProvider; import org.glassfish.jersey.filter.LoggingFilter; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.Test; import static org.junit.Assert.*; import static org.junit.matchers.JUnitMatchers.*; public class LocalDateTest extends JerseyTest { public static class LocalDateParamProvider implements ParamConverterProvider { @Inject private javax.inject.Provider<UriInfo> uriInfoProvider; @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (rawType != LocalDate.class) { return null; } return new ParamConverter<T>() { @Override public T fromString(String value) { UriInfo uriInfo = uriInfoProvider.get(); String format = uriInfo.getQueryParameters().getFirst("date-format"); if (format == null) { throw new WebApplicationException(Response.status(400) .entity("date-format query parameter required").build()); } else { try { return (T) LocalDate.parse(value, DateTimeFormatter.ofPattern(format)); // parse and return here } catch (Exception ex) { throw new WebApplicationException( Response.status(400).entity("Bad format " + format).build()); } } } @Override public String toString(T value) { return value.toString(); } }; } } @Path("localdate") public static class LocalDateResource { @GET public String get(@QueryParam("since") LocalDate since) { return since.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); } } @Override public ResourceConfig configure() { return new ResourceConfig(LocalDateResource.class) .register(LocalDateParamProvider.class) .register(new LoggingFilter(Logger.getAnonymousLogger(), true)); } @Test public void should_return_bad_request_with_bad_format() { Response response = target("localdate") .queryParam("since", "09/20/2015") .queryParam("date-format", "yyyy/MM/dd") .request().get(); assertEquals(400, response.getStatus()); assertThat(response.readEntity(String.class), containsString("format yyyy/MM/dd")); response.close(); } @Test public void should_return_bad_request_with_no_date_format() { Response response = target("localdate") .queryParam("since", "09/20/2015") .request().get(); assertEquals(400, response.getStatus()); assertThat(response.readEntity(String.class), containsString("query parameter required")); response.close(); } @Test public void should_succeed_with_correct_format() { Response response = target("localdate") .queryParam("since", "09/20/2015") .queryParam("date-format", "MM/dd/yyyy") .request().get(); assertEquals(200, response.getStatus()); assertThat(response.readEntity(String.class), containsString("09/20/2015")); response.close(); } }
这是测试依赖项
<dependency> <groupId>org.glassfish.jersey.test-framework.providers</groupId> <artifactId>jersey-test-framework-provider-grizzly2</artifactId> <version>${jersey2.version}</version> <scope>test</scope> </dependency>