问题
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)
回答1:
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.
回答2:
I experienced this called-twice phenomenon because BrowserSync replayed HTTP requests in each open BrowserSync browser window.
回答3:
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 :)
回答4:
Had the same problem.
Eventually I found that I have a null in a background-image url like so:
style="background-image: url(null);"
that caused to send another GET with a path variable null.
回答5:
I have the same problem and find a lot of solution and finally I found the simple reason. It's in my html template css: background:url() . This cold will run the same url again. So I just remove it out or put url in the bracket and it works.
回答6:
Sounds like an issue on client side.
Open up your browser, enter
<host/port/whatever_you_need_to access_the_app>/demo/account/profile/f91b3a38-6921-41e0-98b7-58dff5cb1152and check the logs. The chances are that you'll see only one entryNow run your client code and check network requests to the service. If you're call the controller from the browser like Chrome F12->Network tab should help.
I know it's a kind of obvious, but I think there is nothing really "unusual" in this controller, so it should be more at the level of general flow. In this case maybe it's the best to trace the HTTP traffic and see how many/when/how does it generate requests to your controller.
来源:https://stackoverflow.com/questions/36325529/spring-controller-method-called-twice