[存在PUT
方法,其任务是更换Customer
的卡。 Customer
类本身与User
类之间的@ OneToOne关系(字段name
,最后一个name
等)一起工作。一切正常,该方法执行其任务,并且没有问题。但是出于某种原因,它会发出警告:
2020-04-26 14:15:14.160 WARN 5340 --- [nio-8080-exec-2] o.springframework.validation.DataBinder : Skipping URI variable 'id' because request contains bind value with same name.
如何解决?
我使用Spring-MVC
+Hibernate
+Jpa
+ PostgreSQL
;
班级用户:
package com.tinychiefdelights.model;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import javax.persistence.*;
@ApiModel
@Data
@Entity
@Table(name = "pg_user", schema = "public")
public class User {
public User() { // Пустой конструктор для Hibernate
}
public User(String name, String lastName, String role,
String login, String password) { // Базовый конструктор
this.name = name;
this.lastName = lastName;
this.role = role;
this.login = login;
this.password = password;
}
// Поля
private @Id
@GeneratedValue
Long id;
@Column(name = "login")
private String login;
@Column(name = "password")
private String password;
@Column(name = "role")
private String role;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
}
客户:
package com.tinychiefdelights.model;
import com.fasterxml.jackson.annotation.*;
import lombok.Data;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
@Table(name = "customer", schema = "public")
public class Customer {
public Customer() { // Пустой конструктор для Hibernate
}
// Поля
// name, lastName, login, password берем от класса User через связи;
private @Id
@GeneratedValue
Long id;
@Column(name = "wallet")
private double wallet;
//Relationships
//
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id", referencedColumnName = "id") // Join without Customer in User class
@NotFound(action = NotFoundAction.IGNORE)
private User user;
//Лист заказов
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
@JsonIgnore // Таким образом я предотвратил рекурсию
private List<Order> orderList;
}
客户服务:
public Customer editCustomer(Long id, User user, double wallet){
Customer customer = customerRepository.getByIdAndUserRole(id, "customer");
customer.setUser(user);
customer.setWallet(wallet);
return customerRepository.save(customer);
}
客户总监:
@PutMapping("/customer/{id}")
Customer editCustomer(@PathVariable Long id, User user, @RequestParam double wallet) {
return customerService.editCustomer(id, user, wallet);
}
由于您将具有ID的用户传递给@PutMapping
,因此也有一个路径变量ID。从Spring MVC模型属性中检查以下代码:
private void bind(ServletRequest request, ServletRequestDataBinder dataBinder) {
MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
if (multipartRequest != null) {
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
}
String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
Map<String, String> uriVars = (Map<String, String>) request.getAttribute(attr);
if (uriVars != null) {
for (Map.Entry<String, String> entry : uriVars.entrySet()) {
if (mpvs.contains(entry.getKey())) {
logger.warn("Skipping URI variable '" + entry.getKey()
+ "' since the request contains a bind value with the same name.");
} else {
mpvs.addPropertyValue(entry.getKey(), entry.getValue());
}
}
}
this.extendDataBinder.doExtendBind(mpvs, dataBinder);
dataBinder.bind(mpvs);
}