[英]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.