I have "transit manager" application. It's a simply app to test how to use Spring framework. I've created endpoint for adding new transit to database (Spring Data) and it works fine. I have added a few transits and now I want to get daily report and here I have problems. This is how endpoint should looks like:
GET http://example.com/reports/daily?start_date=YYYY-MM-DD&end_date=YYYY-MM_DD
And response(in JSON) for example:
{
"total_distance": "90",
"total_price": "115"
}
What I suppose to do? Should I add new fields "startDate" and "endDate" in model class?
This is how my model class looks:
@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;
}}
Repository class:
public interface TransitRepository extends JpaRepository<Transit, Long> {
List<Transit> findAllByStartDateGreaterThanEqualAndEndDateLessThanEqual(DateTime startDate, DateTime endDate);
}
Controller class:
@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(){}
}
And service class:
@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){
}
}
You should use the annotaion @RequestParam
on the parameters you need to add to your method. The name in the annotaion is optional and defaults to the parameter name.
@RequestMapping(path = "reports/daily", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
public void getDailyReport(@RequestParam("start_date") String startDate, @RequestParam String end_date){}
}
Adding the fields to your Transit
class would not work as the GET-Request has no request body and the parameters are not in the body anyway.
You should use @RequestParam
annotation in your controller method.
@RequestMapping(path = "reports/daily", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
public void getDailyReport(@RequestParam("start_date") String startDate, @RequestParam("end_date") String endDate){
// Create a method in service class, that takes startDate and endDate as parameters.
// And inside that method, you can write a SQL query to find the distance & price for the given date parameters
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.