when using redirect in spring mvc its redirecting to null

I have the following code :

package org.directi.code.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.ObjectMapper;
import org.directi.code.Snippet;
import org.directi.code.SnippetDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

public class HomeController {

    SnippetDao snippetDao;
    @RequestMapping(value = "/")
    public String newSnippet(HttpServletResponse response) throws IOException {
     return "new";

    @RequestMapping(value = "/snippets", method = RequestMethod.POST)
    public String createSnippet(@RequestBody String jsonString) throws IOException {
        Snippet snippet = new ObjectMapper().readValue(jsonString, Snippet.class);
       String uuid =  snippetDao.insertData(snippet);
       return "redirect:/snippets/"+uuid;


    @RequestMapping(value="/snippets/{id}", method=RequestMethod.GET)
    public String showSnippet(@PathVariable String id)
        Snippet snippet = snippetDao.getSnippetById(id);
        return "snippet";


Open in new window

The problem is when the redirection happens in createSnippet function it calls the showSnippet function.
And then i see an error and strangely url is directed to :
I think there is no problem with snippet.jsp
because if i place it under newSnippet instead of "new" it runs fine.
Something wrong going on with redirection.

What could be the issue.
Rohit BajajAsked:
Who is Participating?

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

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.

Think you need to make use of RedirectAttributes

Change your method to accept RedirectAttributes and populate the attributes with the uuid before returning to a view.

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
Rohit BajajAuthor Commented:
I got the issue.
Actually there was a js error.
It was expecting the response header to contains Location attribute.
which i was not passing and so the page was directing to null..

I did the following and it worked :
        response.addHeader("Location", "http://" + request.getHeader("Host") + "/snippets/" + uuid);
So the redirection was happening from inside the javascript code.

Thanks for help...
Ok, please remove my answer as the accepted solution since it will be misleading when people revisit this question.
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

From novice to tech pro — start learning today.