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.

6
  • 1
    As the error says, that isn't a valid URL. You would need to URL-escape the JSON content before sending it, or (far better), use a POST and put the JSON in the request body. Commented Mar 20, 2017 at 11:57
  • I can't encode the JSON. Commented Mar 20, 2017 at 11:59
  • I can't do any change in the request parameters or request type. Commented Mar 20, 2017 at 12:00
  • I need to somehow be able to accept those values.Any suggestion for that Commented Mar 20, 2017 at 12:02
  • 1
    This question maybe helpful: stackoverflow.com/questions/41053653/… In short you might not have an option but to downgrade to an older Tomcat version if the client is not escaping the query param. Commented Mar 20, 2017 at 12:30

4 Answers 4

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"}]}]
Sign up to request clarification or add additional context in comments.

5 Comments

I am not the one who is sending the request.I cannot change the request format .
Check it out the solution proposed in this post stackoverflow.com/a/21689084/2633386
still giving me Exception
what exception are you getting? is that same or spring related ? in case any configuration issue check the comment in the link above
Spring MVC 4, @RequestBody Map<String, Object> map works with the input json {"name":"frank","Type":"Lumber"}.
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.

3 Comments

I cannot change request type from GET
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.
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.
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)

7 Comments

I am not looking for parsing the JSONString to Object but rather to accept JSON in my query parameter
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
I cannot change that .I could rather change the parameter into JSONObject
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: stackoverflow.com/questions/1547899/…
Http standard does not block the use of above values.Please read the entire q&a
|
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);
    //
}

2 Comments

Throws illegal argument exception for the above URL
@jonesjalapat I think problem with your maven dependencies. Please re check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.