1

I'm new to spring boot. I have a JSON Object that looks like this:

{
    id: 3,
    messageType: ["one", "two", "three"]
}

I have a class that represents the object:

public class Subscription {
    public Subscription(@JsonProperty("id") long id, @JsonProperty("messageType") List<String> messageType) {
        this.id = id;
        this.messageType = messageType;
    }
}

I have a controller with a PUT request that works perfectly:

    @RequestMapping(value=SUBSCRIBE_URI, method=RequestMethod.PUT)
    public ResponseEntity<String> updateSubscription(@RequestBody Subscription payload) throws Exception{
        ...
    }

But I can't get this working at all for the GET request. When I use @RequestParam and separate the id and messageType parameters, the messageType list has brackets in the strings (i.e. "[one]", "[two]"). When I use @RequestBody similar to the PUT request, I get 400 errors.

What is the correct way to pass this JSON data to the GET request without getting brackets in the strings?

Grim
  • 1,938
  • 10
  • 56
  • 123
user1695758
  • 173
  • 1
  • 3
  • 14
  • Possible duplicate of [Passing JSON data in get request as request body](http://stackoverflow.com/questions/11575947/passing-json-data-in-get-request-as-request-body) – Avinash Feb 18 '17 at 05:42

2 Answers2

0

@RequestParam can map only primitive datatypes to their respective variables properly, especially in the case of GET, when it receives all data as simple Strings. Also, I don't see the need of messageType to be of List datatype. As a String, it will simply receive values as plain strings, without brackets.

Utkarsh Mishra
  • 480
  • 1
  • 7
  • 23
0

You can't send JSON on the request parameter, directly. You'll need to do something like call encodeURIComponent() on the json structure you want to pass to your server and then have the argument just be a string. On the server side you need to convert the string back into your model object.

Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42