【问题标题】:REST with Jersey 1.x using interfaces. How toREST 与 Jersey 1.x 使用接口。如何
【发布时间】:2014-02-24 13:38:32
【问题描述】:

我已经使用 JERSEY 1.5 设计了所需的 REST 服务,使用声明的接口如下:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public IMyDTO doIt(ICustomer customer) {
    return MySupport.createDTO(customer);
}

如果我尝试使用 JERSEY 1.5 Client API 调用此服务,则会出现以下错误:

无法构造 test.ICustomer 的实例,问题:抽象类型要么需要映射到具体类型,要么具有自定义反序列化器,要么使用额外的类型信息进行实例化

目前我由客户指定这样的参数:

ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, 
                                                               Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource webResource = client.resource(restResource);
ClientResponse response = webResource.path(restResourcePath)
                    .type(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .post(ClientResponse.class, customer);
if (response != null) {
if (response.hasEntity()) {
result = response.getEntity(IMytDTO.class);
}
....

ICustomerIMyDTOjava 接口,没有 任何 jackson 注释。实现也没有杰克逊注解。

我的问题:如果使用 REST 服务(客户端),如何(反)序列化对象?

我不会更改 java 接口并使用服务和客户端的实现...

【问题讨论】:

    标签: java json rest jersey


    【解决方案1】:

    如果您不愿意使用具体类而不是抽象类,我认为您必须创建自定义序列化器和反序列化器。杰克逊的例子是:

    http://blog.palominolabs.com/2012/06/05/writing-a-custom-jackson-serializer-and-deserializer/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2023-04-08
    • 2023-03-11
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多