How to send an Array to a web service using the new API rest full

25 views
Skip to first unread message

Rubén V

unread,
Oct 26, 2020, 11:14:13 PM10/26/20
to CodenameOne Discussions
Hi,

I have a webservice in "Spring Boot" and I need to know how to send an array through the new REST API.

Thanks

Rubén V

unread,
Oct 27, 2020, 3:13:22 PM10/27/20
to CodenameOne Discussions
Hi,

I don't think I can explain it well, I mean to send an array from my app to my webservice so that later it updates the database.

I have already used the parameters and the body (to send a record), but I need to send an array.

Thanks

Shai Almog

unread,
Oct 27, 2020, 10:42:08 PM10/27/20
to CodenameOne Discussions
Hi,
how do you generate the JSON and communicate with Spring Boot?

If you're using properties we have a fetch as list but not a post as list method. Normally I just create a business object representing the list so:

public MyList implements PropertyBusinessObject {
// ..
     public final PropertyList<MyList, MyObject> listOfObjects = ...
//...

Rubén V

unread,
Oct 28, 2020, 5:42:15 PM10/28/20
to CodenameOne Discussions
Hi,
If I want to send a record from my app to update the database, I do it like this:

    public static Response adicionOrden2(Orden2 p) {
        Response<String> r = Rest.post(SERVER_URL + "orden/adicionOrden2").
                header("token", UsuarioService.getToken()).
                jsonContent().
                body(p.getPropertyIndex().toJSON()).getAsString();
        return r;
    }

How can I send an ArraList?

Shai Almog

unread,
Oct 28, 2020, 11:30:52 PM10/28/20
to CodenameOne Discussions
You have two options:

  public static Response adicionOrden2(Orden2List p) {
        Response<String> r = Rest.post(SERVER_URL + "orden/adicionOrden2").
                header("token", UsuarioService.getToken()).
                jsonContent().
                body(p.getPropertyIndex().toJSON()).getAsString();
        return r;
    }

Then Orden2List is just a class with a PropertyList in it.

The second option is:

    public static Response adicionOrden2(Orden2[] p) {
        StringBuilder array = new StringBuilder("[");
        boolean first = true;
        for(Orden2 current : p) {
             if(!first) {
                  array.append(",");
                  first = false;
             }
             array.append(p.getPropertyIndex().toJSON()).getAsString());
        }
       array.append("]");
        Response<String> r = Rest.post(SERVER_URL + "orden/adicionOrden2").
                header("token", UsuarioService.getToken()).
                jsonContent().
                body(array.toString()).getAsString();
        return r;
    }
Reply all
Reply to author
Forward
0 new messages