它通过休息Web服务发送时的空映射

时间:2017-12-18 13:07:53

标签: java rest web-services

我有一个简单的Object,它包含一个String和一个Map<String, Object>定义如下:

我的对象

@XmlRootElement
public class WebServicePatternObject {

    private @Getter @Setter String code;
    private @Getter @Setter Map<String, Object> fieldsMap = new HashMap<>();

    public WebServicePatternObject() {
    }

    public WebServicePatternObject(String code, Map<String, Object> fieldsMap) {
        this.code = code;
        this.fieldsMap = fieldsMap;
    }
}

客户端

public String getResult(String paramValues, Map<String, Object> mapFieldsValues) 
         throws Exception {
    try {
        Client client = Client.create();

        String uriTarget = 
                   "http://10.6.6.6:8080/admin/ressources-ws-rest/patterns/lastSequence";
        WebServicePatternObject wspo = 
                                new WebServicePatternObject(paramValues, mapFieldsValues);
        WebResource webResource = client.resource(uriTarget);

        //{"code":"CODE_PA","fieldsMap":{"code_1":123456,"code_2":"abcd"}}
        System.out.println(new ObjectMapper().writeValueAsString(wspo));

        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, new ObjectMapper().writeValueAsString(wspo));

        if (response.getStatus() != 201) {
            throw new Exception("Exception Occured - HTTP Error Code :" 
                                 + response.getStatus());
        }

        String output = response.getEntity(String.class);
        //This return SUCCESS_RESULT
        System.out.println(output);
        return output;
    } catch (Exception ex) {
        throw new Exception("Something wrong!");
    }
}

服务方

@Path("/patterns")
public class PatternsWebService {

    @POST
    @Path("/lastSequence")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response getLastSequence(WebServicePatternObject wspo) {
        //WebServicePatternObject{code=CODE_PA, fieldsMapAP={}} <<----------Empty Map, Why?
        System.out.println(wspo);
        return Response.status(201)
                .entity("SUCCESS_RESULT")
                .build();
    }
}

当我用:{/ p>打电话给getResult(String paramValues, Map<String, Object> mapFieldsValues)

Map<String, Object> map = new HashMap<>();
map.put("code_1", 123456);
map.put("code_2", "abcd");
getResult("CODE_PA", map);

服务响应为SUCCESS_RESULT,但服务中的地图为空fieldsMapAP={}

我的问题是,为什么String发送成功但是Map是空的,我想我的代码中遗漏了一些东西,有什么想法吗?

1 个答案:

答案 0 :(得分:0)

  

作为评论的答案添加。这可能是问题的答案

作为答案添加,因为不知道如何在评论上插入图像

我更愿意与您分享一个干净的maven存储库,但我正在使用一个插件项目。该项目还有像这样的灰熊网络服务器和gson。你可以在里面挑选你需要的东西。我相信你关于杰克逊版本的问题。

    aopalliance-repackaged-2.5.0-b30
    grizzly-framework-2.3.28
    grizzly-http-2.3.28 
    grizzly-http-server-2.3.28 
    hk2-api-2.5.0-b30
    hk2-locator-2.5.0-b30 
    hk2-utils-2.5.0-b30 
    jackson-annotations-2.5.4
    jackson-core-2.5.4 
    jackson-databind-2.5.4 
    jackson-jaxrs-base-2.5.4
    jackson-jaxrs-json-provider-2.5.4
    jackson-module-jaxb-annotations-2.5.4 
    javassist-3.20.0-GA
    javax.annotation-api-1.2 
    javax.inject-2.5.0-b30 
    javax.ws.rs-api-2.0.1
    jersey-client-2.25 
    jersey-common-2.25
    jersey-container-grizzly2-http-2.25 
    jersey-entity-filtering-2.20
    jersey-guava-2.25
    jersey-media-jaxb-2.25
    jersey-media-json-jackson-2.20 
    jersey-server-2.25
    osgi-resource-locator-1.0.1
    validation-api-1.1.0.Final

服务器端依赖

Server Side dependencies

客户端依赖

Client Side dependencies