當自定義Provider類時,如日期轉換的Provider,java
package soc.config; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.ws.rs.WebApplicationException; import javax.ws.rs.ext.ParamConverter; import javax.ws.rs.ext.ParamConverterProvider; import javax.ws.rs.ext.Provider; public class DateConverterProvider implements ParamConverterProvider{ public class DateParameterConverter implements ParamConverter<Date> { public static final String format = "yyyy-MM-dd"; // set the format to whatever you need @Override public Date fromString(String string) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); try { return simpleDateFormat.parse(string); } catch (ParseException ex) { throw new WebApplicationException(ex); } } @Override public String toString(Date t) { return new SimpleDateFormat(format).format(t); } } @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (Date.class.equals(rawType)) { return (ParamConverter<T>) new DateParameterConverter(); } return null; } }
除了在本身的配置類裏register該類,如:spring
package soc.config; //import org.codehaus.jackson.jaxrs.JacksonJsonProvider; import org.glassfish.jersey.filter.LoggingFilter; //import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spring.scope.RequestContextFilter; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; public class ApplicationApi extends ResourceConfig { public ApplicationApi() { // 註冊數據轉換器 register(JacksonJsonProvider.class); // 註冊日誌 register(LoggingFilter.class); // register application resources register(UserResource.class); // register filters register(RequestContextFilter.class); // register mine exception class ,to find the trace of exception register(DebugMapperException.class); // register my DateConverterProvider register(DateConverterProvider.class); } }
也能夠在DateConverterProvider的類聲明上部加@Provider註解,如:json
package soc.config; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.ws.rs.WebApplicationException; import javax.ws.rs.ext.ParamConverter; import javax.ws.rs.ext.ParamConverterProvider; import javax.ws.rs.ext.Provider; @Provider public class DateConverterProvider implements ParamConverterProvider{ public class DateParameterConverter implements ParamConverter<Date> { public static final String format = "yyyy-MM-dd"; // set the format to whatever you need @Override public Date fromString(String string) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); try { return simpleDateFormat.parse(string); } catch (ParseException ex) { throw new WebApplicationException(ex); } } @Override public String toString(Date t) { return new SimpleDateFormat(format).format(t); } } @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (Date.class.equals(rawType)) { return (ParamConverter<T>) new DateParameterConverter(); } return null; } }
這樣有多個相似的Provider類時,能夠使用jax-rs的自動路徑掃描,即指定全部的Provider類全部的包名(soc.config),以下:oracle
package soc.config; //import org.codehaus.jackson.jaxrs.JacksonJsonProvider; import org.glassfish.jersey.filter.LoggingFilter; //import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spring.scope.RequestContextFilter; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; public class ApplicationApi extends ResourceConfig { public ApplicationApi() { // 註冊數據轉換器 register(JacksonJsonProvider.class); // 註冊日誌 register(LoggingFilter.class); // register filters register(RequestContextFilter.class); // register mine exception class ,to find the trace of exception register(DebugMapperException.class); // register my DateConverterProvider // register(DateConverterProvider.class); packages("soc.config,soc.resource");// 等價於packages(true, "soc.config"); } }
可是這裏要注意的是,自定義的Provider類裏必定要有合適的構造器,經過個人簡單測試,是要有個無參構造才行,否則會報錯:Could not find a suitable constructor in ……Provider class。app
有人不推薦使用jax-rs的路徑掃描功能,據其說容易出現bug,參考文章以下:ide