Move to the form a ListObject from JS to Server

1

I have a form on a web page to which I collect some values from JS and add them to an object. All those objects I put them into an array and add it to a hidden input I have on the page:

document.editRouteForm.busStops.value = JSON.stringify(getBusStopsPoints());

 var getBusStopsPoints = function () {
        var arrayPos = 0;
        busStops.map(function (marker) {
            //Recogemos las rutas
            busStopsObj.lat = marker.getPosition().lat();
            busStopsObj.lng = marker.getPosition().lng();

            //Recogemos la descripcion
            busStopsObj.description = busStopsInfo[arrayPos];
            arrayPos++;

            console.log("arrayPos: " + arrayPos);
            busStopsForm.push(busStopsObj);
        });

        console.log(busStopsForm);
    };

The output I get is the following:

[
   {
      "lat":43.4721537946863,
      "lng":-3.8495182526509097,
      "description":"2"
   },
   {
      "lat":43.4721537946863,
      "lng":-3.8495182526509097,
      "description":"2"
   },
   {
      "lat":43.4721537946863,
      "lng":-3.8495182526509097,
      "description":"2"
   }
]

Now on the server it comes to me as null and it gives me an exception:

  

[Field error in object 'route' on field 'busStops': rejected value [undefined]; codes [typeMismatch.route.busStops, typeMismatch.busStops, typeMismatch.java.util.List, typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [route.busStops, busStops]; arguments []; default message [busStops]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'busStops'; nested exception is java.lang.IllegalStateException: Can not convert value of type 'java.lang.String' to required type 'en.ticnor.trayecbus.model.BusStop' for property 'busStops [0]': no matching editors or conversion strategy found]]

I am trying to add the values of an object in the back-end with the data that comes from the front. One of those values is a List < > of another object. Which is where the server gives me the exception.

private final List<BusStop> busStops = new ArrayList<>();

And the object is formed by these fields:     public class BusStop extends BaseEntity {

private String description;

private float lat;

private float lng;

The form works and the parameters arrive to him except that field that apparently does not correspond with the expected (an ArrayList of objets). So someone knows why I get this fault? Maybe the output I get from the form with the expected one on the server does not correspond?

Greetings and thanks in advance.

    
asked by Eduardo 10.05.2018 в 09:20
source

1 answer

1

The error tells you that you are trying to convert a String into a List<BusStop> :

Cannot convert value of type 'java.lang.String' to required type 'es.ticnor.trayecbus.model.BusStop'

If you are passing a String in JSON to @Controller , use a parser like Jackson to process it and convert it to the object or list of objects you want.

With the JSON that you send to @Controller :

[
   {
      "lat":43.4721537946863,
      "lng":-3.8495182526509097,
      "description":"2"
   },
   {
      "lat":43.4721537946863,
      "lng":-3.8495182526509097,
      "description":"2"
   },
   {
      "lat":43.4721537946863,
      "lng":-3.8495182526509097,
      "description":"2"
   }
]

Assuming you have a class called BusStop:

public class BusStop {

    private double lat;
    private double lng;
    private String description;
    public BusStop() {

    }

    public BusStop(double lat, double lng, String description) {
        this.lat = lat;
        this.lng = lng;
        this.description = description;
    }

    //getters and setters ....    
}

In this class I do the processing:

package com.so.victor;

import java.io.IOException;
import java.util.List;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class TestSO {

    private static String json = "[{\"lat\":43.4721537946863,\"lng\":-3.8495182526509097,\"description\":\"2\"},{\"lat\":43.4721537946863,\"lng\":-3.8495182526509097,\"description\":\"2\"},{\"lat\":43.4721537946863,\"lng\":-3.8495182526509097,\"description\":\"2\"}]";

    public static void main(String[] args) {
        List<BusStop> mylist = convertBusStop();

        mylist.forEach(c -> {
            System.out.println(c.getLat());
        });
    }

    private static List<BusStop> convertBusStop() {

        ObjectMapper mapper = new ObjectMapper();

        List<BusStop> myObjects = null;
        try {
            myObjects = mapper.readValue(json,
                    mapper.getTypeFactory().constructCollectionType(List.class, BusStop.class));
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return myObjects;

    }
}

I used this version of jackson:

<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
    
answered by 10.05.2018 / 09:35
source