0

Here is the controller method which i'm trying to redirect from:

@GetMapping("/opensignup/{id}")
public String openSignUp(@PathVariable(value = "id") Long Id) {

    Tournament comp = tournamentRepository.findOne(Id);
    boolean b = true;
    comp.setSignUpOpen(b);

    return "redirect:/comp/" + Id;
}

I have another method (which is a post, if thats relevant) which successfully redirects to the correct page/controller with the same line:

   return "redirect:/comp/" + Id;

Instead of redirecting, it simply prints that link on the browser. (It prints it with the correct id.) Like this:

redirect:/comp/5

How do I get it to redirect instead of print?

Benny
  • 51
  • 4
Lucia
  • 55
  • 8
  • Do you have a `@ResponseBody` annotation somewhere in your second called method? – Bennett Dams Apr 24 '18 at 16:12
  • since you mento a browser, you can do it with JS. just catch the response and open it like `location.href= link`, if will post ans if you need after this – parlad Apr 24 '18 at 16:15
  • @BennettDams no, but I just realised where I said it worked before the class has a @ conntroller annotation, but in this class it had @ RestController annotation. Could this be causing it? – Lucia Apr 24 '18 at 16:25
  • @Lucia Yes, I'll post an answer asap. – Bennett Dams Apr 24 '18 at 16:27
  • @BennettDams It turns out the @ RestController was causing it, thanks. – Lucia Apr 24 '18 at 16:29
  • Very gracious of you to spare me the downvote, magnanimous friend. I found no stack over flow questions with the same problem. I Quadruple checked the syntax of " return "redirect:/comp/" + Id;" compared to other stack over flow answers with similar redirects, I didn't see any point in linking to those since the syntax was correct. If referring to those would have helped someone answer my question, then my apologies. – Lucia Apr 24 '18 at 21:16

2 Answers2

1

As stated in the comments, your second method is annotated with @RestController. While @Controller is meant to return views in the Spring context, @RestController is meant to return something that will be written to the response body directly (JSON in most cases).

@RestController is basically a combination of the @Controller AND a @ResponseBody annotation, whereby the second will try to map the POJO to JSON.

Further reading:

https://www.genuitec.com/spring-frameworkrestcontroller-vs-controller/

Difference between spring @Controller and @RestController annotation

Bennett Dams
  • 6,463
  • 5
  • 25
  • 45
0

This was happening because the top of my method had an @RestController annotation instead of @Controller. Since I don't need it to be restful, I changed it and my problem is solved. If anyone wants to shed some light on why this is and/or how you would do it while keeping the @RestController annotation (if possible) then feel free.

Lucia
  • 55
  • 8