Solved

Handling json in url param

Posted on 2016-08-11
1
62 Views
Last Modified: 2016-08-13
HI,
In my spring web application i am having the following code :
 @RequestMapping(value = "")
    public ModelAndView newNote(@RequestParam(value = "noteEvent", required = false) String noteEvent, HttpServletResponse response, HttpServletRequest request) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        String chatId = "";
        if(noteEvent != null) {
            Bun bun = mapper.readValue(noteEvent, Bun.class);
            chatId = bun.getChat();
        }

Open in new window

This is bascially a get request handler and url will have noteEvent param which is actually a json string :
"name":"sdfsd","button":"sdfsd","userId":"u:guid1","userName":"sdfsd","chat":"u:sdf","chatName":"Ben sdf"}

If the requestBody was a json string i could have directly used it as a param to the function and since spring auto performs the conversion i would have got an object withouth having to deal with this mapper thing.
But here as the noteEvent is a url param i had to resort to using this approach.
Dont know if there is a way in spring to handle this.

Please suggest a better approach if possible.

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 41753623
I think the better approach would be to not have the JSON in the URL to begin with. As you mentioned, I would only have it as the request body. If you don't want in the body, then I would break out those JSON values into either URL query params or path params.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question