简体   繁体   中英

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)

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.

我经历了这种称为两次的现象,因为BrowserSync在每个打开的 BrowserSync 浏览器窗口中重放 HTTP 请求。

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 :)

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-58dff5cb1152 and check the logs. The chances are that you'll see only one entry

  • Now 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.

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 .

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.

This might also occur due to one more reason. Because i found samething and observed following.

  • 1st time its when your request processed and you see println statement in Console. And if you refresh browser at method request of controller method ( example http://localhost:8080/DemoMVC/add?***) each refresh your tomcat processes request again and you get same println statement in console.

Perhaps this is too late. However, I still face these issues and forget the solution every time.

In case you are using any JS library like Angular or React then in your service call observe the response as well.

Here is a code snippet

return this.http.get<User>(`${this.resourceUrl}/activate`, { params: options, observe: 'response' })
  .pipe(
    filter((response: HttpResponse<User>) => response.ok),
    map((response: HttpResponse<User>) => response.body),
    catchError(error => {
      return of(error)
    })
  );

The key area to focus is { params: options, observe: 'response' }

I had a controller which was listening to localhost/ and a get method which matched on a path variable something like this:

@GetMapping("/{foo}")
String get(@PathVariable(required = false) String foo)
{
    return "hello world";
}

And the problem was, that after calling localhost/ I got the first call, and after that, I got the second call for the favicon.

The same would be true if you would define a context root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM