Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Handling json in url param

Posted on 2016-08-11
1
Medium Priority
?
89 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
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

972 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