0

I have to read JSON values from the URL using a query parameter for GET request. I am using Tomcat latest in a Spring Boot project.

       @RequestMapping(
            value = "/values",
            method = RequestMethod.GET, 
            headers = HttpHeaders.ACCEPT + "=" + MediaType.APPLICATION_JSON_VALUE,
            produces = "application/json")
       public ResponseEntity<String> myMethod(
            @RequestParam(value="key") String jsonRequestString) {
       //parse JSONString 
       //--
       }

GET request to the URL

Url:- http://localhost:port/values?key={"org":"AA","points":[{"name":"xy","Measures":"343"}]}]

Throws java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

EDIT :- Sending JSON in original form leading me to nowhere, so working approach would be to send the JSON in an encoded form.

jones j alapat
  • 931
  • 3
  • 13
  • 26

4 Answers4

2

You can encode the key parameter to Base64 format and append to the URL, then decode and use it in your controller method.

Key JSON text:

{"org":"AA","points":[{"name":"xy","Measures":"343"}]}] 

Base64 encoded text:

eyJvcmciOiJBQSIsInBvaW50cyI6W3sibmFtZSI6Inh5IiwiTWVhc3VyZXMiOiIzNDMifV19XQ==

Decoded to again back to JSON text:

{"org":"AA","points":[{"name":"xy","Measures":"343"}]}]
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Aditya
  • 41
  • 1
  • 4
1

Few things you should take care of.

  1. Use POST HTTP method to post your JSON to server.
  2. Create a JAVA pojo class which should has a same structure as your JSON. Like for below JSON,

      {
            "id": 123,
            "status": "Ordered",
            "product": "Pepsi"
           } 

i would create a class( You can create a two separate class as you has array inside json) ,

public class Order {

private long id ;
private String status; 
private String product ;

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getProduct() {
    return product;
}
public void setProduct(String product) {
    this.product = product;
} 

}

  1. Then,

       @RequestMapping(value = "/values", method = RequestMethod.POST,produces={"application/json"},
                consumes={"application/json"})
        public ResponseEntity myMethod(
                    @RequestBody Order orderObj) {
        }

Please take reference from this example and build the solution.

ProgrammerBoy
  • 876
  • 6
  • 19
  • I cannot change request type from GET – jones j alapat Mar 20 '17 at 12:18
  • that not a ideal way to send JSON formatted parameter in URL. Either you have to encode it or use POST method. By any way if you get the GET working then also you will end up in some other issue because what you are expecting is not ideal way to handle JSON. – ProgrammerBoy Mar 20 '17 at 12:43
  • yes.I understand your reservations.The thing is that this works seamlessly in our other application in different Technology.So somehow i need to do this in the exactly same way. – jones j alapat Mar 20 '17 at 12:56
0

You can directly pass String into the JSONObject constructor

There are lots of libraries available.

1.http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm (JSON jar)

2.http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm (Simple JSON)

3.http://www.java2s.com/Code/Jar/g/Downloadgson224sourcesjar.htm (GSON from google)

I personally use JSON and GSON jars.

JSONObject jsonObject = new JSONObject(variable)
Stenal P Jolly
  • 737
  • 9
  • 20
  • I am not looking for parsing the JSONString to Object but rather to accept JSON in my query parameter – jones j alapat Mar 20 '17 at 12:04
  • Put your RHS of your parameter in quotes and encode before sending to the server. You can decode the text on the server side and convert to any format you wish – Stenal P Jolly Mar 20 '17 at 12:07
  • I cannot change that .I could rather change the parameter into JSONObject – jones j alapat Mar 20 '17 at 12:11
  • What do you mean "I cannot change that". The HTTP standard is the http standard, and if it says you can only use certain chars, that's that: http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid – slim Mar 20 '17 at 12:20
  • Http standard does not block the use of above values.Please read the entire q&a – jones j alapat Mar 20 '17 at 12:27
  • Well, you've got four "unsafe" characters (`{}[]`) and one "reserved" character (`:`) in your parameter -- reserved characters are only permitted when "used for their reserved purposes". – slim Mar 20 '17 at 13:16
  • But more to the point, although you can interpret the RFCs however you like, the implementors of Spring and Tomcat have taken a particular view, and they have deemed these characters as invalid. – slim Mar 20 '17 at 13:20
0

You can simply try something like this

@GetMapping(value = "/values", produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<String> testMethod(@RequestParam("key") String key) throws Exception {
    JSONObject jsonObject = new JSONObject(key);
    //
}
Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50