简体   繁体   中英

BeanMapping: Replace value in set within mapping expression?

I have a string Set I'm mapping into a bean for a PDF form content extraction:

@Mapping(target = "targetFieldName", expression = "java(contentMapperService.convertStringToSet(pdfFieldsMap.get(\"PDF_field_name\")))")

Let's say the input String from the PDF field is "apple,banana"

If we find the value "apple", we want to replace it with "apples" before the set is mapped to the target.

Is this possible to do within an expression?

You can create a method with your specific logic and use it inside the expression

Cause you don't provide the mapper, take this as an example.

@Mapper(componentModel = "spring")
public abstract class MyMapper {

    @Mapping(target = "targetFieldName", expression = "java(setTargetFieldName(source.getPdfFieldName()))")
    public abstract Target toTarget(Source source);

    protected String setTargetFieldName(String pdf_field_name){
        return pdf_field_name.replace("apple", "apples");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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