无法从按日期列出的数据类型中获取项目(获取空白页面)

时间:2018-04-13 12:41:27

标签: java spring spring-boot

我在尝试访问此链接时遇到任何数据存在问题:

http://localhost:8080/transits/2017-10-22

当我得到

http://localhost:8080/transits

数据显示:

[{"date":"3917-11-21","source_adress":"WASS","destination_adress":"SS","price":450},{"date":"3917-11-21","source_adress":"DASS","destination_adress":"DD","price":450},{"date":"3917-11-21","source_adress":"MASS","destination_adress":"CC","price":450}]

大家好,即时尝试使用Spring启动编写API。你可以帮我解决我的代码错误,所以我不能从“约会”过渡吗?我添加了

spring.jackson.date-format=yyyy-MM-dd 

但这对我没有帮助。即使我想要输入我的json数据格式“3917-11-21”也没有任何反应。提前致谢。 Program tree

这是我的TransportDAO

   package com.dariusz.Dao;

    import com.dariusz.Entity.Transport;
    import org.springframework.stereotype.Repository;
    import java.util.Collection;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;

    @Repository
    public class TransportDao {

        private static final Map<Integer, Transport> transports = new HashMap<Integer, Transport>() {
           {
                put(1, new Transport("WASS", "SS", 450,new Date(2017,10,22)));
                put(2, new Transport("DASS", "DD", 450,new Date(2017,10,22)));
                put(3, new Transport("MASS", "CC", 450,new Date(2017,10,22)));
            }
        };

        public Collection<Transport> getAllTransports(){
            return this.transports.values();
        }

        public Transport getTransportByDate(Date date){
            return this.transports.get(date);
        }

    }

TransportService

package com.dariusz.Service;

import com.dariusz.Dao.TransportDao;
import com.dariusz.Entity.Transport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;

@Service
public class TransportService {

    @Autowired
    private TransportDao transportDao;

    public Collection<Transport> getAllTransports(){
        return this.transportDao.getAllTransports();
    }

    public Transport getTransportByDate(Date date){
        return this.transportDao.getTransportByDate(date);
    }
}

Transport.java

package com.dariusz.Entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Transport {


    @JsonFormat(pattern="yyyy-MM-dd")
    private Date date;
    private String source_adress;
    private String destination_adress;
    private int price;
    public Transport(String source_adress, String destination_adress, int price, Date date) {
        this.source_adress = source_adress;
        this.destination_adress = destination_adress;
        this.price = price;
        this.date = date;
    }
    public Transport(){}

    public String getSource_adress() {
        return source_adress;
    }

    public void setSource_adress(String source_adress) {
        this.source_adress = source_adress;
    }

    public String getDestination_adress() {
        return destination_adress;
    }

    public void setDestination_adress(String destination_adress) {
        this.destination_adress = destination_adress;
    }

    public int getPrice() {
        return price;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;

    }

}

TransportController

package com.dariusz.Controller;

import com.dariusz.Entity.Transport;
import com.dariusz.Service.TransportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

@RestController

@RequestMapping("/transits")

public class TransportController {
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
    @Autowired
    private TransportService transportService;

    @RequestMapping(method = RequestMethod.GET)
    public Collection<Transport> getAllTransports(){
        return transportService.getAllTransports();
    }

    @RequestMapping(value = "/{date}", method = RequestMethod.GET)
    public @ResponseBody Transport getTransportByDate(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(required = false) Date date){
        return transportService.getTransportByDate(date);

    }

}

2 个答案:

答案 0 :(得分:0)

如果您使用的是Java 8(或更高版本),请使用较新的类型/类,如果仅限日期,则为LocalDate;如果包含时间,则使用LocalDateTime。这是旧的遗留日期时间类的替代品,例如java.util.Date,Calendar,&amp; SimpleDateFormat的。

public class Transport {


    @JsonFormat(pattern="yyyy-MM-dd")
    private LocalDate date;

    //code remove for brevity
}

答案 1 :(得分:0)

我可能错了,但......

public Transport getTransportByDate(Date date){
      return this.transports.get(date);
}

应使用地图date中的密钥transports返回传输。但是,在您的情况下,密钥可以是123。我认为问题出在这里。

您可以放置​​一个扩展存储库,其中包含extends JpaRepository<Transports, Integer>(或mongo或您的数据库)或者juste做一个

public Transport getTransportByDate(Date date){
        for (Map.Entry<String, Transport> entry : this.transport.entrySet()){
                if (date.equals(entry.getValue().getDate()) {
                        return entry.getValue();
                }
        }
}

(这不是优雅的,但这个想法就在这里)。