简体   繁体   English

Spring 控制器方法调用了两次

[英]Spring controller method called twice

I'm making a Spring MVC web app.我正在制作一个 Spring MVC Web 应用程序。 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):在控制台中(写入 ulr 请求):

/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在第二次调用 tihs 方法后,它会进入我的视图

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. LE:我在评论中也说过。 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) (在第二次调用中,id 为 0,模型为空)

I have also observed one GET request causing the controller method to execute twice.我还观察到一个 GET 请求导致控制器方法执行两次。 The problem occurred when requesting the service using a Chrome browser (the problem did not occur when using Postman).使用 Chrome 浏览器请求服务时出现问题(使用 Postman 时未出现问题)。 In my case the culprit was the JSONView Chrome extension.就我而言,罪魁祸首是 JSONView Chrome 扩展。

I determined the cause by using the Network tab of the Chrome developer tools.我通过使用 Chrome 开发人员工具的网络选项卡确定了原因。 It showed my GET service being requested two times.它显示我的 GET 服务被请求了两次。 The second request was initiated by content.js, which is a JavaScript file bundled with JSONView.第二个请求由 content.js 发起,这是一个与 JSONView 捆绑在一起的 JavaScript 文件。

After I disabled the JSONView extension, a GET request through Chrome would cause the controller method to execute only once.在我禁用 JSONView 扩展后,通过 Chrome 的 GET 请求将导致控制器方法只执行一次。

我经历了这种称为两次的现象,因为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 :)我用@RequestParam 替换了@PathVariable,并且没有两次访问该URL :)

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.打开浏览器,输入<host/port/whatever_you_need_to access_the_app>/demo/account/profile/f91b3a38-6921-41e0-98b7-58dff5cb1152并查看日志。 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.如果您从 Chrome F12-> 网络选项卡之类的浏览器调用控制器应该会有所帮助。

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.在这种情况下,最好跟踪 HTTP 流量并查看它有多少/何时/如何向您的控制器生成请求。

Had the same problem.有同样的问题。 Eventually I found that I have a null in a background-image url like so:最终我发现我在背景图像 url 中有一个null ,如下所示:

style="background-image: url(null);"

that caused to send another GET with a path variable null .这导致发送另一个带有路径变量null的 GET 。

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() .它在我的 html 模板 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.所以我只需将其删除或将 url 放在括号中即可。

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.第一次处理您的请求并且您在控制台中看到 println 语句时。 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.如果您在控制器方法的方法请求处刷新浏览器(例如 http://localhost:8080/DemoMVC/add?***),则每次刷新您的 tomcat 处理请求时,您都会在控制台中获得相同的 println 语句。

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.如果您使用任何 JS 库,如 Angular 或 React,那么在您的服务调用中也要观察响应。

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' }要关注的关键领域是{ 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:我有一个正在监听localhost/的控制器和一个匹配路径变量的 get 方法,如下所示:

@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.问题是,在调用localhost/之后,我接到了第一个电话,之后,我接到了第二个电话图标。

The same would be true if you would define a context root.如果您要定义上下文根,情况也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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