Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring controller method called twice

I'm making a Spring MVC web app. The problem is that on single method is called twice and I don't know why.

@RequestMapping(value="/profile/{id}", method = RequestMethod.GET)
public String displayUserProfile( @PathVariable String id) {

    System.out.println("asdasddsasd");

    return "account/userProfile";

}

I commented many line from this method, but is still not working. Also tried to return other view..no good luck.

In console(ulr requests are written):

/demo/account/profile/f91b3a38-6921-41e0-98b7-58dff5cb1152
asdasddsasd
/demo/account/profile/0
asdasddsasd

After the second call of tihs method, it's going to my view

Any other method work fine. Does anyone know what's the problem here?

*I also read similar question from here..nothing helped

LE: what I also said in the comments. What is funny is that, if I set o model to the view, on the second call of the method, my view get's the model from the first call. (on the second call, with id 0, the model is null)

like image 795
UnguruBulan Avatar asked Mar 31 '16 06:03

UnguruBulan


People also ask

Can we have 2 controller class in spring boot?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.

How many instances does a spring controller have?

Spring controllers are singletons (there is just one instance of each controller per web application) just like servlets.

How does Spring MVC handle multiple controllers?

2 Answers. Show activity on this post. @Controller @RequestMapping("user") public class UserController { @RequestMapping("edit") public ModelAndView edit(@RequestParam(value = "id", required = false) Long id, Map<String, Object> model) { ... } }


3 Answers

I have also observed one GET request causing the controller method to execute twice. The problem occurred when requesting the service using a Chrome browser (the problem did not occur when using Postman). In my case the culprit was the JSONView Chrome extension.

I determined the cause by using the Network tab of the Chrome developer tools. It showed my GET service being requested two times. The second request was initiated by content.js, which is a JavaScript file bundled with JSONView.

After I disabled the JSONView extension, a GET request through Chrome would cause the controller method to execute only once.

like image 172
Mark Norman Avatar answered Sep 29 '22 19:09

Mark Norman


I experienced this called-twice phenomenon because BrowserSync replayed HTTP requests in each open BrowserSync browser window.

like image 45
Abdull Avatar answered Oct 01 '22 19:10

Abdull


I finally got some time to find a solution here. Tried many things, but it didn't worked.

I replaced @PathVariable with @RequestParam and the URL is not accessed twice :)

like image 28
UnguruBulan Avatar answered Sep 30 '22 19:09

UnguruBulan