简体   繁体   English

如何将时间值从邮递员发送到我的 REST api

[英]How to send time value from Postman to my REST api

I have a class with a variable of type Date, representing a time我有一个带有 Date 类型变量的类,表示时间

@Entity
public class Product implements Serializable {

private static final long serialVersionUID = -7181205262894478929L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int productId;

@NotNull()
private String productName;

@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "yyyy-MM-dd")
@NotNull()
private Date date;

@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@NotNull()
private Date time;
....
}

Now I'm trying CRUD methods on Postman, and when I send现在我正在 Postman 上尝试 CRUD 方法,当我发送

{ "productName": "name", "date": "2016-03-10", "time": "10:29" } { "productName": "name", "date": "2016-03-10", "time": "10:29" }

I get我得到

400 Bad Request 400 错误请求

with a description:附有说明:

The request sent by the client was syntactically incorrect.客户端发送的请求在语法上不正确。

When I try without time, it passes.当我没有时间尝试时,它就过去了。

If you are using Jackson, you can try the following solutions:如果您使用的是 Jackson,您可以尝试以下解决方案:

1. Using a custom JsonDeserializer 1. 使用自定义的JsonDeserializer

Define a custom JsonDeserializer :定义一个自定义JsonDeserializer

public class TimeDeserializer extends JsonDeserializer<Date> {

    private SimpleDateFormat format = new SimpleDateFormat("hh:mm");

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) 
        throws IOException, JsonProcessingException {

        String date = p.getText();

        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

Then just annotate your time attribute with @JsonDeserialize :然后只需使用@JsonDeserialize注释您的time属性:

@NotNull
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@JsonDeserialize(using = TimeDeserializer.class)
private Date time;

2. Using the @JsonFormat annotation 2.使用@JsonFormat注解

Alternativelly, you could try the @JsonFormat annotation, instead of creating a custom JsonDeserializer :或者,您可以尝试使用@JsonFormat注释,而不是创建自定义JsonDeserializer

@NotNull
@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="hh:mm")
private Date time;

One last thing最后一件事

Is hh:mm the format you really want? hh:mm是您真正想要的格式吗?

hh means hours in 1-12 format while HH means hours in 0-23 format . hh表示1-12 格式的小时数,而HH表示0-23 格式的小时数。 If you go for the 1-12 format, you could consider using the AM/PM marker : hh:mm a .如果您选择 1-12 格式,您可以考虑使用AM/PM 标记hh:mm a

For more details, have a look at the SimpleDateFormat documentation.有关更多详细信息,请查看SimpleDateFormat文档。

change this改变这个

@Temporal(TemporalType.TIME)
@DateTimeFormat(style = "hh:mm")
@NotNull()
private Date time;

instead of代替

@NotNull()
private String time;

because you try parse to String value 10:29 not a valid representation for your time variable因为您尝试解析为字符串值10:29不是您的time变量的有效表示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何捕获其余api控制器上的文件以及使用Postman发送它的方法 - how to catch a file on the rest api controller and what method to send it with Postman 我想将列表(它是对象的成员)从 Postman 发送到 Spring REST ZDB974238714CA8ACED638 - I want to send a List(which is a member of an object) from Postman to Spring REST API 我正在使用 Postman 将数据传递给 REST api 但我的变量显示 Z37A6259CC0C1DAE0BDZA 值 - I am using Postman, to pass the data to a REST api but my variable is showing null value 如何从 Postman rest 客户端发送 spring csrf 令牌? - How do I send spring csrf token from Postman rest client? 如何使用Postman从curl发布REST - How to Post a REST from curl using Postman 如何从邮递员发送 JSON 以获取自定义 VO - How to send JSON from postman for a custom VO 如何发送 Stream<string> 作为来自 REST API 的客户的响应?</string> - How to send Stream<String> as response to client from REST API? 如何阻止从 Postman/其他 API 的/等访问我的 API .. (Spring Boot) - How to block access to my API from Postman/Other API's/etc.. (Spring Boot) 如何在rest控制器spring boot中为邮递员生成api-key - how to generate api-key for postman in rest controller spring boot 如何在POST方法的REST API中发送日期 - How to send Date in REST API in POST method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM