Solved

Handling json in url param

Posted on 2016-08-11
1
66 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 36

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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