Best way to create a json

HI,
In my spring mvc application i am creating a json like :
 @RequestMapping(value = "/snippets", method = RequestMethod.POST)
    public void createSnippet(@RequestBody String jsonString, HttpServletResponse response, HttpServletRequest request) throws IOException {
        Snippet snippet = new ObjectMapper().readValue(jsonString, Snippet.class);
        String uuid = snippetDao.insertData(snippet);
        response.addHeader("Location", "http://" + request.getHeader("Host") + "/snippets/" + uuid);
        response.setStatus(201);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        String snippetURL = snippetURL(uuid, "/raw");
        String filename = snippetFilename(snippet.getTitle(), snippet.getMode());
        int size = snippet.getText().length();
        String json = "{\"download\" : {\"URL\": \""+snippetURL+"\",\"filename\":\""+filename+"\",\"size\":"+size+"}}";
        response.getWriter().write(json);
    }

Open in new window


In the above code this is the line i am talking about :        String json = "{\"download\" : {\"URL\": \""+snippetURL+"\",\"filename\":\""+filename+"\",\"size\":"+size+"}}";
Althouth its just one line... but wondering if there is a better way to write this ?
Also in the line        response.getWriter().write(json);
Now this json is referred in the ajax call in the javascript code... So i am passing it this way... Please
comment on this... if this is a correct way to do this ?
This code is in controller... Should this be better moved to service ?
Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
I think the json code is ok to have in the controller. Keep the service operating on your domain objects.

As for constructing json. It is valid to construct as a string or you could use a API such as gson to do it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.