Vee*_*ndi 2 rest date jax-rs resteasy jboss7.x
这是我想要做的:
@POST
@Path("/MyPath")
@Produces("text/xml")
@RolesAllowed({"user"})
public Output myMethod (@QueryParam("itemId") long ItemId,
@QueryParam("plannedstartdate") Calendar plannedStartDate,
@QueryParam("plannedreturndate") Calendar plannedReturnDate)
Run Code Online (Sandbox Code Playgroud)
我正在使用JBoss AS7.据我所知,resteasy已集成到JBoss AS7中.我能够运行简单的休息服务.
我找到的关于传递日期的唯一文档位于以下链接:http: //docs.jboss.org/resteasy/2.0.0.GA/userguide/html/StringConverter.html#StringParamUnmarshaller
由于说明不明确,我无法遵循此并解决问题.当我尝试创建示例中给出的注释DateFormat时,它无法识别StringParamUnmarshaller.我不知道从哪里得到它.如果resteasy已经集成到JBoss AS7中,这不应该被识别吗?
我的pom.xml具有以下依赖项:
<!-- Import the JAX-RS API, we use provided scope as the API is included
in JBoss AS 7 -->
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
对此方法的调用失败,因为不会发生字符串到日历的转换.我不想传递String而不是Calendar,因为有其他客户端直接调用java.任何人都可以帮助我如何将日期传递给Rest Calls?
谢谢Veer
此问题已得到解决.请参阅以下代码.
创建一个Annotation类CalendarFormat.java:
@Retention(RUNTIME)
@StringParameterUnmarshallerBinder(CalendarFormatter.class)
public @interface CalendarFormat {
String value();
}
Run Code Online (Sandbox Code Playgroud)
添加一个类CalendarFormatter.java:
public class CalendarFormatter implements StringParameterUnmarshaller<Calendar> {
private SimpleDateFormat formatter;
public void setAnnotations(Annotation[] annotations) {
CalendarFormat format = FindAnnotation.findAnnotation(annotations, CalendarFormat.class);
formatter = new SimpleDateFormat(format.value());
}
public Calendar fromString(String str) {
try {
Calendar cal = Calendar.getInstance();
cal.setTime(formatter.parse(str));
return cal;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
添加到POM
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.3.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>2.3.4.Final</version>
<exclusions>
<exclusion>
<artifactId>resteasy-jaxrs</artifactId>
<groupId>org.jboss.resteasy</groupId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
更改方法签名以使用注释
@POST
@Path("/MyPath")
@Produces("text/xml")
@RolesAllowed({"user"})
public Output myMethod(@QueryParam("itemId") long ItemId,
@QueryParam("plannedstartdate") @CalendarFormat("MM-dd-yyyy") Calendar plannedStartDate,
@QueryParam("plannedreturndate") @CalendarFormat("MM-dd-yyyy") Calendar plannedReturnDate)
Run Code Online (Sandbox Code Playgroud)
而已.
| 归档时间: |
|
| 查看次数: |
3882 次 |
| 最近记录: |