9

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)

UnguruBulan
  • 890
  • 4
  • 12
  • 24

9 Answers9

10

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.

Mark Norman
  • 2,271
  • 1
  • 13
  • 17
  • I HAD SAME PROBLEM, i dont know which extension it was ... just disabled most... I had some SnappySnipped that one could be it.. – Tomas Bisciak Jan 19 '19 at 17:31
  • 2
    In my case, Network tab shows only one GET request made but I still get two hits on Spring side... strange. – schlingel Feb 08 '19 at 11:01
  • You could look at the embedded server log to verify that the application is receiving only one request. Here's a stack overflow chain that discusses embedded server logging in Spring ... https://stackoverflow.com/questions/48312851/spring-boot-embedded-tomcat-logs – Mark Norman Feb 08 '19 at 21:33
  • Maybe, it's Chrome in combination with BrowserSync!? The same problem with a PUT request. First it sent the new data, 200-800 ms later it sent the old data, which made my changes disappear. But as soon as I closed the Chrome developer tools view, it disappeared... – Dirk Jul 05 '19 at 11:15
3

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

Abdull
  • 26,371
  • 26
  • 130
  • 172
  • Maybe, it's Chrome in combination with BrowserSync!? The same problem with a PUT request. First it sent the new data, 200-800 ms later it sent the old data, which made my changes disappear. But as soon as I closed the Chrome developer tools view, it disappeared... – Dirk Jul 05 '19 at 11:17
  • In my case it was because I had the same page opened twice in 2 different browsers while running BrowserSync. – Paris P May 27 '20 at 09:21
1

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

UnguruBulan
  • 890
  • 4
  • 12
  • 24
  • May be due to CORS enablement ? However I have the same issue and still not found a solution therefore i cannot accept this as answer. – Tim Nov 11 '16 at 16:45
0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Already tried first thing, but it's still called twice. I will try your second advice – UnguruBulan Mar 31 '16 at 06:48
  • It is certanly not, I am using soap ui and browser network tab from developer tools, so I know only one request goes out. – Tim Nov 11 '16 at 16:44
0

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.

MagicKriss
  • 150
  • 13
0

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.

  • Consider elaborating what you mean by "So I just remove it out or put url in the bracket and it works." a little so that your new answer can be more useful to users in the future. – entpnerd Oct 11 '19 at 05:34
0

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.
Arshad
  • 1
0

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' }

dharam
  • 7,882
  • 15
  • 65
  • 93
0

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.

Valerij Dobler
  • 1,848
  • 15
  • 25