validate json payload in spring boot rest to throw error if unknown property or empty property has been sent as part of json payload

[亡魂溺海] 提交于 2020-08-26 06:52:51

问题


JSON Request:

{
    "notificationType" : "ISSUER_OTP1ee2asasa",
    "content" : "hi fff this is fff template content for SBI email good and mobile dfdfdfd and remaining balance is 333 and your name is hahaha.",
    "medium" : "EMAIL",
    "asa":"ddddd",
    "":""
}

POJO:

package com.innoviti.notification.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;


@Document(collection = "NotificationTemplate")
@JsonIgnoreProperties(ignoreUnknown=false)

public class NotificationTemplate {
  @JsonCreator
  public NotificationTemplate(@JsonProperty(value="notificationType",required=true)String notificationType, 
      @JsonProperty(value="content",required=true)String content, @JsonProperty(value="medium",required=true)String medium) {
    super();
    this.notificationType = notificationType;
    this.content = content;
    this.medium = medium;
  }

  @Override
  public String toString() {
    return "NotificationTemplate [id=" + id + ", templateId=" + templateId + ", notificationType="
        + notificationType + ", content=" + content + ", medium=" + medium + "]";
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  @Id
  private String id;
  private String templateId;

  public String getTemplateId() {
    return templateId;
  }

  public void setTemplateId(String templateId) {
    this.templateId = templateId;
  }

  private String notificationType;
  private String content;
  private String medium;

  public String getMedium() {
    return medium;
  }

  public void setMedium(String medium) {
    this.medium = medium;
  }

  public String getNotificationType() {
    return notificationType;
  }

  public void setNotificationType(String notificationType) {
    this.notificationType = notificationType;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }


}

Controller where payload is posted.

 @PostMapping(value = "/config", consumes = MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<NotificationTemplate> configureTemplate(
     @Valid @RequestBody NotificationTemplate notificationTemplate) {
    NotificationTemplate notificationTemplatePersisted = null;

    logger.info(
        "Printing payload of template on server side" + ">>>" + notificationTemplate.toString());
    try {
      validatePayLoad(notificationTemplate);
      notificationTemplatePersisted =
          notificationTemplateService.createNotificationTemplate(notificationTemplate);
    } catch (Exception de) {
      logger.info(String.format("Error in saving template", de.getMessage()));
      throw new RequestNotCompletedException(de.getLocalizedMessage());
    }
    return new ResponseEntity<NotificationTemplate>(notificationTemplatePersisted,
        HttpStatus.CREATED);
  }

Question:How do I validate that an uknown property has been sent as part of payload.In Existing implementation,@RequestBody maps the json without any issue.I want to throw error or validate payload if incoming json is not confirming exactly to POJO.For e.g in payload example i gave,I want to be able to throw error saying that asa is not recognized property


回答1:


The Jackson property that controls this behaviour is FAIL_ON_UNKNOWN_PROPERTIES. This needs to be true in your case, to get the behaviour you describe.

It seems that since spring boot 1.2 this is set to false by default.

To set it to true add this line to your application.properties file:

spring.jackson.deserialization.fail-on-unknown-properties=true

And then you will get a JsonMappingException when there are extraneous properties in a JSON payload




回答2:


One can add this class int their project and it would throw an exception if json is mismatched to the pojo class properties.

 @Configuration
public class Config implements InitializingBean {

    @Autowired
    private RequestMappingHandlerAdapter converter;

    @Override
    public void afterPropertiesSet() throws Exception {
        configureJacksonToFailOnUnknownProperties();
    }

    private void configureJacksonToFailOnUnknownProperties() {
        MappingJackson2HttpMessageConverter httpMessageConverter = converter.getMessageConverters().stream()
                                                                            .filter(mc -> mc.getClass()
                                                                                            .equals(MappingJackson2HttpMessageConverter.class))
                                                                            .map(mc -> (MappingJackson2HttpMessageConverter) mc)
                                                                            .findFirst()
                                                                            .get();



        httpMessageConverter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }
}


来源:https://stackoverflow.com/questions/44900719/validate-json-payload-in-spring-boot-rest-to-throw-error-if-unknown-property-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!