Redirect to @ResponseBody?

I have something to login ajax through spring security:

@RequestMapping(value="/entry.html", method=RequestMethod.GET)
public String getEntry(){
    return isAuthenticated()? "redirect:/logout.jsp":"entry";
}

@RequestMapping(value="/postLogout.html", method=RequestMethod.GET)
public @ResponseBody String getPostLogout(){
    return "{success:true, logout:true, session:false}";
}

The flow is that when it receives a call in /entry.html, it checks and selects

  • redirect to /logout.jsp=> using spring security and then redirect to /postLogout.html=> JSON response
  • allow view entry, which is jsp, contains only json string

I would like to know if I can use @ResponseBodyin getEntry()without using jsp to write only json values?

+3
source share
1 answer

The easiest way to return JSON from Spring is through Jackson .

I would create a custom return object:

public class MyReturnObject{
private boolean success;
private boolean session;
private boolean logout;
// + getters and setters
// + 3-arg constructor
}

:

@RequestMapping(value="/postLogout.html", method=RequestMethod.GET)
@ResponseBody 
public MyreturnObject getPostLogout(){
    return new ReturnObject(true,true,false);
}

Spring + JSON mime. . .

+1

All Articles