gpt4 book ai didi

java - Spring MVC Rest - URL 中的多个参数

转载 作者:行者123 更新时间:2023-12-01 19:55:16 25 4
gpt4 key购买 nike

我有“交通管理器”应用程序。这是一个简单的应用程序,用于测试如何使用 Spring 框架。我已经创建了用于将新传输添加到数据库(Spring Data)的端点,并且它工作正常。我添加了一些过境,现在我想获取每日报告,但我遇到了问题。端点应如下所示:

获取 http://example.com/reports/daily?start_date=YYYY-MM-DD&end_date=YYYY-MM_DD

响应(JSON 格式)例如:

{
"total_distance": "90",
"total_price": "115"
}

我应该做什么?我应该在模型类中添加新字段“startDate”和“endDate”吗?

这就是我的模型类的外观:

@Entity
public class Transit {

public Transit() {
}

@Column(name = "id")
@Id
@GeneratedValue
private Long id;
@NotEmpty
private String sourceAddress;
@NotEmpty
private String destinationAddress;
private double price;
private DateTime date;
private Long distance;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getSourceAddress() {
return sourceAddress;
}

public void setSourceAddress(String sourceAddress) {
this.sourceAddress = sourceAddress;
}

public String getDestinationAddress() {
return destinationAddress;
}

public void setDestinationAddress(String destinationAddress) {
this.destinationAddress = destinationAddress;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public DateTime getDate() {
return date;
}

public void setDate(DateTime date) {
this.date = date;
}


public Long getDistance() {
return distance;
}

public void setDistance(Long distance) {
this.distance = distance;
}}

存储库类:

public interface TransitRepository extends JpaRepository<Transit, Long> {
List<Transit> findAllByStartDateGreaterThanEqualAndEndDateLessThanEqual(DateTime startDate, DateTime endDate);
}

Controller 类:

@RestController
@RequestMapping("/transit")
public class TransitController {
private TransitService transitService;

@Autowired
public void setTransitService(TransitService transitService) {
this.transitService = transitService;
}


@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void addTransit(@RequestBody Transit transit){
transitService.calculateDistance(transit);
transitService.addTransit(transit);
System.out.println(transit);
}

@RequestMapping(path = "reports/daily", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
public void getDailyReport(){}

}

和服务等级:

@Service
public class TransitService {

private TransitRepository transitRepository;
public static final String API_KEY = "xxxxxx";

@Autowired
public void setTransitRepository(TransitRepository transitRepository) {
this.transitRepository = transitRepository;
}

public void addTransit(Transit transit) {
transitRepository.save(transit);
}

public void calculateDistance(Transit transit) {

GeoApiContext geoApiContext = new GeoApiContext.Builder().apiKey(API_KEY).build();
DistanceMatrixApiRequest request = DistanceMatrixApi.newRequest(geoApiContext);

DistanceMatrix result = request.origins(transit.getSourceAddress())
.destinations(transit.getDestinationAddress())
.mode(TravelMode.DRIVING)
.units(Unit.METRIC)
.awaitIgnoreError();

long distance = result.rows[0].elements[0].distance.inMeters;
transit.setDistance(distance);

}

public void countDaily(List<Transit> transitList){

}
}

最佳答案

您应该在需要添加到方法中的参数上使用注释@RequestParam。注解中的名称是可选的,默认为参数名称。

@RequestMapping(path = "reports/daily", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
public void getDailyReport(@RequestParam("start_date") String startDate, @RequestParam String end_date){}

}

将字段添加到 Transit 类中不起作用,因为 GET-Request 没有请求正文,并且参数也不在正文中。

关于java - Spring MVC Rest - URL 中的多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49862500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com