Java 如何将日期类型参数作为字符串放入GET请求中?格式";yyyy-MM-dd“; 这是我的用户实体 下面是无法工作的GET请求。我只能将startDate和endDate作为字符串发送,但我需要它们采用日期格式。 @GetMapping(path=“/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}”) 公共响应属性getPositionByUserAndTimePeriod(@PathVariable长用户ID、@PathVariable日期开始日期、@PathVariable日期结束日期){ 返回新的ResponseEntity(新的ArrayList(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK); } 这是一个有效的更新GET请求 @GetMapping(path=“/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}”) public ResponseEntity getPositionByUserAndTimePeriod(@PathVariable Long userId, @PathVariable@DateTimeFormat(pattern=“yyyy-MM-dd”)LocalDate startDate, @PathVariable@DateTimeFormat(pattern=“yyyy-MM-dd”)LocalDate-endDate{ 返回新的ResponseEntity(新的ArrayList(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK); } 这是职位服务 @覆盖 公共列表getPositionByUserAndTimePeriod(长用户ID、LocalDate startDate、LocalDate endDate){ 用户=新用户(); user.setId(userId); return positionRepository.findAllByUserAndCreationDateBetween(用户、开始日期、结束日期); } 这是位置库 列出findAllByUserAndCreationDateBetween(用户用户、LocalDate开始日期、LocalDate结束日期); 你对我应该如何解决这个问题有什么想法吗?
GET请求应该是什么样子的?我应该在服务或其他地方修改某些内容吗Java 如何将日期类型参数作为字符串放入GET请求中?格式";yyyy-MM-dd“; 这是我的用户实体 下面是无法工作的GET请求。我只能将startDate和endDate作为字符串发送,但我需要它们采用日期格式。 @GetMapping(path=“/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}”) 公共响应属性getPositionByUserAndTimePeriod(@PathVariable长用户ID、@PathVariable日期开始日期、@PathVariable日期结束日期){ 返回新的ResponseEntity(新的ArrayList(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK); } 这是一个有效的更新GET请求 @GetMapping(path=“/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}”) public ResponseEntity getPositionByUserAndTimePeriod(@PathVariable Long userId, @PathVariable@DateTimeFormat(pattern=“yyyy-MM-dd”)LocalDate startDate, @PathVariable@DateTimeFormat(pattern=“yyyy-MM-dd”)LocalDate-endDate{ 返回新的ResponseEntity(新的ArrayList(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK); } 这是职位服务 @覆盖 公共列表getPositionByUserAndTimePeriod(长用户ID、LocalDate startDate、LocalDate endDate){ 用户=新用户(); user.setId(userId); return positionRepository.findAllByUserAndCreationDateBetween(用户、开始日期、结束日期); } 这是位置库 列出findAllByUserAndCreationDateBetween(用户用户、LocalDate开始日期、LocalDate结束日期); 你对我应该如何解决这个问题有什么想法吗?,java,mysql,spring,Java,Mysql,Spring,GET请求应该是什么样子的?我应该在服务或其他地方修改某些内容吗 谢谢你抽出时间 必须向参数添加@DateTimeFormat(pattern=“yyy-MM-dd”)注释: @GetMapping(path = "/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}") public ResponseEntity<List<Position>> getPositionByUserAndTime
谢谢你抽出时间 必须向参数添加
@DateTimeFormat(pattern=“yyy-MM-dd”)
注释:
@GetMapping(path = "/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}")
public ResponseEntity<List<Position>> getPositionByUserAndTimePeriod(@PathVariable Long userId,
@PathVariable Date startDate,
@PathVariable @DateTimeFormat(pattern="yyy-MM-dd") Date endDate) {
return new ResponseEntity<>(new ArrayList<Position>(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK);
}
@GetMapping(path=“/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}”)
public ResponseEntity getPositionByUserAndTimePeriod(@PathVariable Long userId,
@路径变量日期开始日期,
@PathVariable@DateTimeFormat(pattern=“yyy-MM-dd”)日期结束日期{
返回新的ResponseEntity(新的ArrayList(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK);
}
尝试yyyy/MM/dd并分别获取每个参数。在控制器中创建日期将DateTimeFormat注释从实体移动到控制器方法参数,在实体中它是无用的,在控制器方法参数中它是需要的。请停止使用过时的日期类。使用java.time.LocalDate.@MandarDharurkar使用分钟而不是月份不会有帮助。你能添加你现在的错误吗?它部分起作用,谢谢。我现在将更新代码,并将错误放在那里。实际上,在我将类型从Date更改为LocalDate everywhere之后,我现在没有收到任何错误。现在可以用了,非常感谢。
package org.scd.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.time.LocalDate;
import java.util.Date;
@Entity
@Table(name = "POSITIONS")
public class Position {
@Id
@GeneratedValue(strategy =GenerationType.IDENTITY)
private Long id;
@Column(name = "START_DATE", nullable = false)
private LocalDate creationDate;
@Column(name = "LATITUDE", nullable = false, length = 45)
private String latitude;
@Column(name = "LONGITUDE", nullable = false, length = 45)
private String longitude;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false, updatable = false)
@JsonIgnore
private User user;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Position )) return false;
return id != null && id.equals(((Position) o).getId());
}
@Override
public int hashCode() {
return 31;
}
public Position() {
}
public Position(LocalDate creationDate, String latitude, String longitude, User user) {
this.creationDate = creationDate;
this.latitude = latitude;
this.longitude = longitude;
this.user = user;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDate getCreationDate() {
return creationDate;
}
public void setCreationDate(LocalDate creationDate) {
this.creationDate = creationDate;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
@GetMapping(path = "/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}")
public ResponseEntity<List<Position>> getPositionByUserAndTimePeriod(@PathVariable Long userId, @PathVariable Date startDate, @PathVariable Date endDate) {
return new ResponseEntity<>(new ArrayList<Position>(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK);
}
@GetMapping(path = "/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}")
public ResponseEntity<List<Position>> getPositionByUserAndTimePeriod(@PathVariable Long userId,
@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
return new ResponseEntity<>(new ArrayList<Position>(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK);
}
@Override
public List<Position> getPositionByUserAndTimePeriod(Long userId, LocalDate startDate, LocalDate endDate) {
User user = new User();
user.setId(userId);
return positionRepository.findAllByUserAndCreationDateBetween(user,startDate,endDate);
}
List<Position> findAllByUserAndCreationDateBetween(User user, LocalDate startDate, LocalDate endDate);
@GetMapping(path = "/byUserIdAndTimePeriod/{userId}/{startDate}/{endDate}")
public ResponseEntity<List<Position>> getPositionByUserAndTimePeriod(@PathVariable Long userId,
@PathVariable Date startDate,
@PathVariable @DateTimeFormat(pattern="yyy-MM-dd") Date endDate) {
return new ResponseEntity<>(new ArrayList<Position>(positionService.getPositionByUserAndTimePeriod(userId,startDate,endDate)),HttpStatus.OK);
}