python requests 请求 java 服务的 Date类型参数

java springboot 的HTTP接口 有Date 类型的参数,python 要访问怎么传Date参数呢?

解决方法如下:

python传字符串参数就可以了

data = {"cityCode": "130400", "lineId": "171", "direction": 0, "list": [{'startTime': "2020-06-23 10:00:00"}],
        "start": "2020-06-23 10:00:00", "end": "2020-06-23 10:00:00",
        "targetDate": "2020-06-23"}
re = requests.post("http://localhost:8087/manual/batchAdd", json.dumps(data),
                   headers={"Content-Type": "application/json"})

 

同时 java 的Date 类型参数要加一个注解  @JsonFormat  ,这样python就可以传yyyy-MM-dd HH:mm:ss格式的字符串参数了

 

    @RequestMapping("/batchAdd")
    public void add(@RequestBody Params params) {

    }
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lty.dispatch.entity.Plan;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
public class Params {

    String cityCode;
    String lineId;
    Integer direction;
    List list;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    Date start;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    Date end;
    String targetDate;

    @Override
    public String toString() {
        return "Params{" +
                "cityCode='" + cityCode + '\'' +
                ", lineId='" + lineId + '\'' +
                ", direction=" + direction +
                ", list=" + list +
                ", start='" + start + '\'' +
                ", end='" + end + '\'' +
                ", targetDate='" + targetDate + '\'' +
                '}';
    }
}

 

你可能感兴趣的:(python requests 请求 java 服务的 Date类型参数)