Link to home
Start Free TrialLog in
Avatar of wrynn
wrynn

asked on

How to serialize array of custom objects and post as json with jax-rs ClientBuilder?

I'm trying to figure out how to serialize an array of java objects and post as json with jax-rs ClientBuilder using jersey and/or jackson.

I'm using the ClientBuilder to do a post. If for example you have an Arraylist of Car objects

List<Car> cars = new ArrayList<>();

Open in new window

And I want to do something like:

Response response = ClientBuilder
.newClient.target(url)
.request()
.accept(MediaType.APPLICATION_JSON)
.post(Entity.json(cars));

Open in new window

and post exactly this as the payload

[{"brand": "toyota"}]

Open in new window

How would I need to implement the Car object? And how would I need to setup my ClientBuilder chain properly?
Avatar of girionis
girionis
Flag of Greece image

You will need

public class Car {
    private String brand;
    // getters and setters
}

Open in new window


and then

List<Car> cars = new ArrayList<>();
Car toyota = new Car();
car.setBrand("toyota");
cars.add(car);

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.