简体   繁体   English

如何防止控制器方法在Chrome浏览器的Spring Boot中运行两次?

[英]How can I prevent controller methods from running twice in spring boot in chrome browser?

I am using spring boot with postgresql to save row in table and print table rows. 我正在使用带有postgresql的spring boot将表中的行保存并打印表中的行。 My problem is that all the WebController methods are being executed twice but only when I open the url in chrome. 我的问题是,所有WebController方法都被执行两次,但仅当我在chrome中打开url时才执行。 If I do this in internet explorer, it works fine. 如果我在Internet Explorer中执行此操作,则效果很好。 How can I stop this in chrome? 如何在Chrome浏览器中停止此操作?

I am putting the code of four files for the reference although I don't think anything's wrong in the code. 我将四个文件的代码作为参考,尽管我认为代码中没有任何问题。 Do I need to change browser settings or something? 我是否需要更改浏览器设置或其他内容?

User.java User.java

@Entity
@Table(name="users")
public class User implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name="name")
    private String name;

    @Column(name="email")
    private String email;

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    @Override
    public String toString() {
        return String.format("User[id=%d, name='%s', email='%s']",this.id,this.name,this.email);
    }
}

UserRepository.java UserRepository.java

public interface UserRepository extends JpaRepository<User, Long>{

}

WebController.java WebController.java

@RestController 
public class WebController {

    @Autowired
    private UserRepository repository;

    @GetMapping("home")
    public String home() {
        System.out.println("whaaat");
        return "hi";
    }

    @GetMapping("/save")
    public String process() {

        repository.save(new User("vidhi","vd@gmail.com"));
        System.out.print("apple ");
        return "Done";
    }

    @RequestMapping("findall")
    public String findAll() {
        String result = "";

        for(User u: repository.findAll()) {
            result += u.toString() + "<br>";
        }

        return result;
    }

}

application.properties application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password= 
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

I got to know that the methods are running twice because of double addition of rows in the table and "whaaat" is printed twice in console but only when in chrome. 我知道方法要运行两次,因为在表中重复添加了两行,并且“ whaaat”在控制台中打印了两次,但仅在chrome中才打印两次。 Any help would be wonderful. 任何帮助都会很棒。

Thanks for your time. 谢谢你的时间。

First. 第一。 Try "Network" tab in Chrome Developer Tools (try F12 button) to identify how many requests are actually sent. 尝试使用Chrome开发者工具中的“网络”标签(尝试按F12按钮)来确定实际发送了多少个请求。 在此处输入图片说明

Second. 第二。 It's generally not a good idea to save any data at "GET" request, because of it's semantic, browser doesn't expect changes on server side and can invoke request multiple times (can't produce a use case for that, but it's totally not forbiden for browser to do that) or get it from cache. 通常,在“ GET”请求中保存任何数据不是一个好主意,因为它的语义,浏览器不希望在服务器端进行更改,而是可以多次调用请求(无法产生用例,但这是完全可以的)不禁止浏览器执行此操作)或从缓存中获取。

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

相关问题 如何在执行测试时阻止我的 Spring Boot Batch 应用程序运行? - How can I prevent my Spring Boot Batch application from running when executing test? 如何解决此问题,我无法从 Postman 或浏览器访问我的 controller class 方法? - How can I resolve this issue, I am unable to access my controller class methods from Postman or Browser? 在这种情况下如何防止方法两次运行? - How to prevent method from running twice in this situation? 如何从客户端的控制器下载zip文件? 春季靴 - How can I download zip file from controller on client side? Spring boot 如何从 Spring Controller 返回一个字节数组作为图像,以便浏览器可以显示它? - How can I return a byte array as an image from a Spring Controller, so that a browser can display it? 如何将Thymeleaf模板中的隐藏值传递给控制器​​(Spring Boot)? - How can I pass a hidden value from my Thymeleaf template to a controller(Spring Boot)? 如何保护用户角色的Spring控制器方法? - How can I secure Spring controller methods for user roles? 如何防止自动解码 Spring controller 中的 RequestParam 字符串 - How can I prevent the automatic decoding of RequestParam Strings in a Spring controller 如何防止用户两次输入相同的文本 - How can I prevent the user from entering the same text twice 无法进入我的Spring Boot Controller方法 - Can't get to my spring boot controller methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM