1

I need to convert 2-D array comes from JSON to a string. I just need to manipulate that value.

There is an example of the JSON:

https://en.wikipedia.org/wiki/GeoJSON

@RequestMapping is the way to manipulate value that comes from REST. But i dont know how to do it for nested object.

    @ResponseBody
    public GeoJson saveData(@RequestParam {What i need}...) throws IOException{
        return geoJSONRepository.save(geoJson);
    }

GeoJson class:

@Entity
public class GeoJson {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String type;
    @OneToMany(cascade = CascadeType.ALL)
    @ToString.Exclude
    private List<Feature> features = new ArrayList<Feature>();
    public GeoJson() {
    }
}

Feature class:

@Entity
public class Feature {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long fId;
    private String type;
    @OneToOne(cascade = CascadeType.ALL)
    private Properties properties;
    @OneToOne(cascade = CascadeType.ALL)
    private Geometry geometry;
}

Properties class:

@Entity
public class Properties {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "pId", nullable = false)
    private Long pId;
    @Column(name = "property_type")
    private String type;
}

Geometry class:

@Entity
public class Geometry {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "gId", nullable = false)
    private Long gId;
    private String type;
    private String coordinates; //2-d coordinates value

}
TranceAddict
  • 187
  • 3
  • 14
  • A questions, is your endpoint GET? Simply stating the object type after `@RequestParam` should work For parameter as big as GeoJSON, I would suggest using `@RequestBody` instead of `@RequestParam` – stark Mar 10 '22 at 08:56
  • On second thought, possible duplicate of: https://stackoverflow.com/questions/16942193/spring-mvc-complex-object-as-get-requestparam – stark Mar 10 '22 at 09:00
  • @stark with `@RequestBody` How could i convert 2-D array to String? Should i create an data transfer object with 2-d array and assign posted JSON to it, then convert it to the entity that contains string coordinates? – TranceAddict Mar 10 '22 at 09:05
  • Check the link to an already asked question. If you just specify they type as `String geoJson` it should automatically translate your incoming param to that `String` object – stark Mar 10 '22 at 09:21
  • @stark i think you misunderstood. The link is not related to my question. I don't want to convert whole object to string. I want to just convert 2-d array to string in geometry class. – TranceAddict Mar 10 '22 at 09:31
  • @TranceAddict Does this answer of your question https://stackoverflow.com/questions/15618823/convert-two-dimensional-array-to-string – Faheem azaz Bhanej Mar 10 '22 at 09:38

0 Answers0