简体   繁体   English

SpringBoot:提供缺少某些字段的 JSON 请求

[英]SpringBoot: Serving JSON requests with some fields missing

I'm building a POJO class to match some requests coming my way in a REST API implemented on SpringBoot.我正在构建一个 POJO 类来匹配在 SpringBoot 上实现的 REST API 中出现的一些请求。 Some of the data has to be given to me, otherwise I am not willing to even serve the request.有些数据必须给我,否则我不愿意,甚至服务请求。 To make sure the client gives me at least what I absolutely need, I have used Lombok's @NonNull :为了确保客户至少给我我绝对需要的东西,我使用了 Lombok 的@NonNull

@Data
public class ProductRequestBody implements Serializable
{
    private String name;
    private String category;
    private String description;
    private String abbreviation;
    private String labelColor;
    private Double cost;

    public ProductRequestBody()
    {
    }

    @JsonCreator
    public ProductRequestBody(@NonNull @JsonProperty("name") String name,
                              @NonNull @JsonProperty("category") String category,
                              @JsonProperty("description") String description,
                              @NonNull @JsonProperty("cost") Double cost)
    {
        this.name = name;
        this.category = category;
        this.description = description;
        this.cost = cost;
    }
}

(I completely understand that handling monetary quantities as Double s is a no-no; this is just an example. (我完全理解将货币数量处理为Double s 是禁忌;这只是一个例子。

Processing this from my controller is as easy as a listener on the /products endpoint like so:从我的控制器处理这个就像/products端点上的侦听器一样简单,如下所示:

@PostMapping(value = "/products")
public Product postProduct(@RequestBody ProductRequestBody newProduct)
{
   // ... 
   // Serve the request appropriately
   // ... 
}

Now, if I receive a POST request with a null field that has not been marked as @NonNull , like the following, I can serve it without issue:现在,如果我收到一个带有标记为@NonNullnull字段的 POST 请求,如下所示,我可以毫无问题地提供它:

{
        "name": "Some Product Name",
        "category": "Some Product Category", 
        "cost" : 10.0,
        "description": null
}

My goal, however, is to be able to handle JSON requests that simply don't even have the fields they don't care about.但是,我的目标是能够处理根本没有他们不关心的字段的JSON请求。 That is, I want to be able to serve the following as well, and I currently can't:也就是说,我也希望能够提供以下服务,但目前我不能:

{
        "name": "Some Product Name",
        "category": "Some Product Category", 
        "cost" : 10.0,
        // No "description" field in this payload
}

How could I go about doing this?我怎么能去做这件事? The less code, the better, as always.代码越少越好,一如既往。

If you use spring-boot and lombok you can simplify your class to be like this:如果您使用 spring-boot 和 lombok,您可以将类简化为如下所示:

@Data
public class ProductRequestBody implements Serializable {

    @NonNull
    private String name;

    @NonNull
    private String category;

    @NonNull
    private Double cost;

    private String description;
    private String abbreviation;
    private String labelColor;
}

it will return 400 if name, category or cost will be not provided and 200 otherwise.如果未提供名称、类别或成本,则返回 400,否则返回 200。

Spring handles serializing and deserializing json without any issue. Spring 可以毫无问题地处理序列化和反序列化 json。 You should let spring handle it.你应该让 spring 处理它。
You can try the following.您可以尝试以下操作。

@Data
public class ProductRequestBody implements Serializable {
    @NonNull
    private String name;
    @NonNull
    private String category;
    private String description;
    private String abbreviation;
    private String labelColor;
    @NonNull
    private Double cost;
}

If you really want to follow the pattern of creating a constructor, then you should create a constructor with only the @NonNull fields and create getter of others ( lombok handles that for you).如果你真的想跟着创建一个构造格局,那么你应该创建一个只有一个构造函数@NonNull领域,创造吸气他人( lombok句柄你)。 If you want to add @JsonProperty then you need to create separate getter.如果要添加@JsonProperty则需要创建单独的 getter。

@Data                                                                                
public class ProductRequestBody implements Serializable                              
{                                                                                    
    private String name;                                                             
    private String category;                                                         
    private String description;                                                      
    private String abbreviation;                                                     
    private String labelColor;                                                       
    private Double cost;                                                             
                                                                                     
    public ProductRequestBody()                                                      
    {                                                                                
    }                                                                                
                                                                                     
    @JsonCreator                                                                     
    public ProductRequestBody(@NonNull @JsonProperty("name") String name,            
                              @NonNull @JsonProperty("category") String category,    
                              @NonNull @JsonProperty("cost") Double cost)            
    {                                                                                
        this.name = name;                                                            
        this.category = category;                                                    
        this.cost = cost;                                                            
    }                                                                                
                                                                                     
    @JsonProperty("description")                                                     
    public String getDescription() {                                                 
        return description;                                                          
    }                                                                                
}                                                                                    
                                                                                     

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM