Jersey ClientResponse.getEntity of generic type

前端 未结 1 1397
孤独总比滥情好
孤独总比滥情好 2020-12-01 11:32

I got a problem to deserialize using jeresy ClientRespone.getEntity

I\'ve tried to follow some tutorials and questions, include this: http://jersey.576304.n2.nabble

相关标签:
1条回答
  • 2020-12-01 12:06

    Few things either need to be fixed or added (not sure in your case as some parts are missing)

    Resource: Made my own to test, as yours was missing a few items

    @Path("/")
    public class AccountResource {
        @GET
        @Path("/account") // route to a specific method.re
        @Produces(MediaType.APPLICATION_JSON)
        public Response saveDataIntoHash() {
            List<Account> accounts = new ArrayList<Account>();
            accounts.add(new Account("Stack", "Savings"));
            accounts.add(new Account("Overflow", "Checkings"));
            GenericEntity generic = new GenericEntity<List<Account>>(accounts){};
            return Response.status(201).entity(generic).build();
        }
    }
    

    Assumed you have this dependency:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey-version}</version>
    </dependency>
    

    Test case: Notice the client configuration. This is needed.

    public void testMyResource() {
        ClientConfig config = new DefaultClientConfig();
        config.getClasses().add(JacksonJaxbJsonProvider.class);
        config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    
        Client c = Client.create(config);
    
        WebResource resource = c.resource(Main.BASE_URI);
        ClientResponse response = resource.path("account")
                .accept("application/json").get(ClientResponse.class);
    
        List<Account> accounts
                    = response.getEntity(new GenericType<List<Account>>(){});
    
        StringBuilder builder = new StringBuilder("=== Accounts ===\n");
        for (Account account: accounts) {
            builder.append("Name: ").append(account.getName()).append(", ")
                    .append("Type: ").append(account.getType()).append("\n");          
        }
        builder.append("==================");
        System.out.println(builder.toString());    
    }
    

    Account (client) class is missing one annotation. It is required as you are using field annotations. Another option is to add a getter and setter for the id

    @XmlRootElement  
    @XmlAccessorType(XmlAccessType.FIELD)  // <======= This Here
    public class Account {
        // added toString for testing
        @Override
        public String toString() {
            return "Account{" + "name=" + name 
                              + ", type=" + type 
                              + ", id=" + id + '}';
        }
    }
    

    Result from test:

    === Accounts ===
    Name: Stack, Type: Savings
    Name: Overflow, Type: Checkings
    ==================
    

    Note: this test is based on the assumption that nothing is wrong on your server side.

    0 讨论(0)
提交回复
热议问题