带集合的java jaxrs messagebodywriter
我已经定义了以下jax的MessageBodyWriter:
@Provider
@ApplicationScoped
@Produces(MediaType.APPLICATION_JSON)
public class CustomerMessageBodyWriter implements MessageBodyWriter<Customer>
....
}
此外,我的rest端点如下所示:
@Path("/api/customers)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class CustomerService {
@GET
@Path("/{customerNumber})
public Customer getCustomer(@PathParam("customerNumber") String customerNumber) {}
@GET
public List<Customer> getCustomers(){}
}
我希望将CustomerMessageWriter用于Customer and List<Customer>
响应选项,但我找不到配置此选项的方法
如果有帮助,我将使用resteasy作为jaxrs实现
注:
我当前的解决方案是检查列表泛型类型。使用完全不同的MessageBodyWriter,似乎我不能使用相同的MessageBodyWriter,除非我与泛型吻别:
public boolean isWriteable(@Nonnull final Class<?> type,
@Nonnull final Type genericType,
@Nonnull final Annotation[] annotations,
@Nonnull final MediaType mediaType) {
if (!List.class.isAssignableFrom(type) || !MediaType.APPLICATION_JSON_TYPE.isCompatible(mediaType)) {
return false;
}
if (!(genericType instanceof ParameterizedType)) {
return false;
}
final ParameterizedType parameterizedType = (ParameterizedType) genericType;
final Type actualType = parameterizedType.getActualTypeArguments()[0];
return actualType == Customer.class;
}
共 (0) 个答案