V
V
vincent_gun2018-12-02 01:04:26
JavaScript
vincent_gun, 2018-12-02 01:04:26

How to make a redirect in a controller with JSON in SpringMVC?

Good time!
A JSON object comes from the front, I accept it in the controller:

@ResponseBody
@PostMapping(value = "controller", consumes = "application/json")
public SomeDto controller(@RequestBody SomeDto someDto, Authentication authentication) {
... 
}

Now SomeDto is completed with session data (from authentication), and all this data should be safely sent to another page. Question: how?
I'm guessing something like this:
@ResponseBody
@PostMapping(value = "controller", consumes = "application/json")
public String controller(@RequestBody SomeDto someDto, Authentication authentication, ModelMap modelMap) {

    String username;
    try {
          username = authentication.getName();
    } catch (Exception e) {
          String message = "Unauthorized user";
          LOG.warn(message);
          return "login";
    }

    UserDto userDto = userService.getUser(username);
    someDto.setUser(userDto);

    modelMap.addAttribute("someDto", someDto);
    return "another-page";
}

Naturally, this is a hat hat, but the meaning is this. How to implement it?
Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2018-12-02
@aol-nnov

https://stackoverflow.com/a/10049138

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question