jones j alapat
jones j alapat

Reputation: 989

How to read JSON in GET request query parameter

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.

Upvotes: 0

Views: 13863

Answers (4)

Aditya
Aditya

Reputation: 41

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"}]}]

Upvotes: 2

Januka samaranyake
Januka samaranyake

Reputation: 2597

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);
    //
}

Upvotes: 0

ProgrammerBoy
ProgrammerBoy

Reputation: 891

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.

Upvotes: 1

Stenal P Jolly
Stenal P Jolly

Reputation: 777

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)

Upvotes: 0

Related Questions