Java JAX-RS:ParamConverterProvider rawType不匹配
在我的REST应用程序中,我有一个Java JAX-RS:ParamConverterProvider rawType不匹配,java,rest,jax-rs,Java,Rest,Jax Rs,在我的REST应用程序中,我有一个@POST方法,它使用x-www-form-urlencoded并生成application/json,旨在为美容中心创建新的预约: @POST @Produces("application/json") @Consumes("application/x-www-form-urlencoded") public Response bookAppointment(@FormParam("date") Date appDate, @FormParam
@POST
方法,它使用x-www-form-urlencoded
并生成application/json
,旨在为美容中心创建新的预约:
@POST
@Produces("application/json")
@Consumes("application/x-www-form-urlencoded")
public Response bookAppointment(@FormParam("date") Date appDate,
@FormParam("time") Time appTime, @FormParam("type") String appType,
@FormParam("clientName") String clientName, @FormParam("email") String clientEmail,
@DefaultValue("") @FormParam("message") String clientMsg) {
//externalize the validation of all fields to concentrate on "positive"
//scenario only
validator(appDate, appType, clientName, clientEmail);
Appointment appointment = build(appDate, appTime, appType,clientName,
clientEmail, clientMsg);
try {
repository.add(appointment);
return Response.ok(appointment).build();
} catch (Exception e) {
throw new InternalServerErrorException("Something happened in the application "
+ "and this apointment could not get saved. Please contact us "
+ "to inform us of this issue.");
}
}
正如您可能看到的,两个端点方法属性是Java对象(sql.Time
和sql.Date
)。为了从来自客户端的字符串中转换它们,我使用了B.Burke的restfuljava with JAX-RS(第70-71页)和本文中所教的ParamConverterProvider
s
我正在使用Postman chrome插件发送请求,如果我正在发送valueOf()的sql.date
和sql.time
(即分别为yyyy-MM-dd
和hh:MM:ss
的生成器方法所需的完整字符串属性,那么一切都可以正常工作。然而,当我发送没有秒的时间时,我收到404异常和一条普通消息
这是我从邮递员那里寄来的:
如果您查看其中一个ParamConverterProvider
,您将看到我正在考虑部分用户输入,并且以任何一种方式为BadRequestException
抛出自定义消息(适用于其他验证约束):
“跨步”将断点移动到返回null,并且对日期转换器提供程序重复相同的操作。由于我正在仔细地遵循这本书中的示例,我不确定为什么rawType
被评估为false您可以System.out.println(rawType.getName())
在IF?有趣的想法之前,我从来没有想到过:-请等一分钟:-)您应该看到打印出的一堆类名与方法的所有参数的类型对应这是输出rawType.getName()信息:long根据您的帖子和评论,您的JAX-RS实现已经在将您传递的字符串转换为日期,这就是为什么您会看到long。您可能不需要任何自定义转换器。尝试使用ParamConverter中的长值:System.out.println(新日期(值))
执行此操作
@Provider
public class SqlTimeConverterProvider implements ParamConverterProvider {
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
System.out.println("SqlTimeConverterProvider");
if (rawType.equals(Time.class)) {
return new ParamConverter<T>() {
@Override
public T fromString(String value) {
//in case the ParamConverter does not do URI deconding
value = value.replaceAll("%3A", ":");
if (value.length() < 6) {
value = value.concat(":00");
}
if (!value.matches("([01]?[0-9]|2[0-3]):[0-5][0-9]"
+ "(:[0-5][0-9])*")) {
throw new BadRequestException(value + " is not an accepted Time format "
+ "please use this pattern: hh:mm:SS");
}
return rawType.cast(Time.valueOf(value));
}
@Override
public String toString(T value) {
Time timeRepr = (Time) value;
if (timeRepr.toLocalTime().getMinute() < 10) {
String reply = timeRepr.toLocalTime().getHour()+":"
+timeRepr.toLocalTime().getMinute();
return reply.concat("0");
}
return timeRepr.toLocalTime().toString();
}
};
}
return null;
}
}
System.out.println("SqlTimeConverterProvider");
if (rawType.equals(Time.class)) {