validate xml:space="preserve" is present in the xml request and include xml:space="preserve" in xml response

Viewed 58

I have an api written in java/spring-boot that consumes json and xml request and produces json and xml response. In the xml request there is a xml:space="preserve" in a specific optional xml element. How can I validate if xml:space="preserve" is present in the specific optional xml element? Also, how can I include xml:space="preserve" in the specific xml element in the response?

Sample XML Request:

<searchStylingPartRequest>          
    <stylingPart>       
        <stylingCategory>EXT</stylingCategory>  
        <stylingPartName xml:space="preserve">EXT BODY*</stylingPartName>   
    </stylingPart>      
</searchStylingPartRequest>

Sample XML Response:

<searchStylingPartResponse>         
    <stylingPart>       
        <stylingPartID>294</stylingPartID>  
        <stylingCategory>EXT</stylingCategory>  
        <stylingPartName xml:space="preserve">EXT BODY (OUTSIDE)</stylingPartName>  
        <stylingPartComment xml:space="preserve">This Styling Part was created  
        by HGW user.</stylingPartComment>   
    </stylingPart>      
</searchStylingPartResponse>

Here is my requestDto and responseDto with annotations for json and xml.

RequestDto:

@Getter
@ToString
@EqualsAndHashCode
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@JsonTypeName("stylingPart")
public class StylingPartDto {

    @Length(max = 27, message = "String length is over the definition.")
    @JsonProperty("stylingCategory")
    @XmlElement(name = "stylingCategory")
    private String stylingCategory;
    
    @Length(max = 90, message = "String length is over the definition.")
    @JsonProperty("stylingPartName")
    @XmlElement(name = "stylingPartName")
    private String stylingPartName;
}

ResponseDto:

@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "stylingPart")
public class SearchStylingPartResponseDto {

    @JsonProperty("stylingPartID")
    @XmlElement(name = "stylingPartID")
    private int stylingPartId;
    
    @JsonProperty("stylingCategory")
    @XmlElement(name = "stylingCategory")
    private String stylingCategory;
    
    @JsonProperty("stylingPartName")
    @XmlElement(name = "stylingPartName")
    private String stylingPartName;
    
    @JsonProperty("stylingPartComment")
    @XmlElement(name = "stylingPartComment")
    private String stylingPartComment;
}
0 Answers
Related