Domanda

So I am working on a solution right now wherein we have 2 requirements:

  1. Format SSN / Telephone Number in Hyphen form which is otherwise currently being displayed without it.
  2. Format an amount field in the format "$0.00".

Currently we have written a method formatAsHyphen and formatAmount as below:

/**
 * This method converts the given string into US format SSN / Telephone number
 * @param valueToFormat
 * @param fieldToFormat , It should be either 'S' for SSN and 'T' for Mobile Number
 * @return
 */
public String formatWithHyphen (String valueToFormat, String fieldToFormat) {
    if(valueToFormat != null && valueToFormat.length() > 1) {
        StringBuilder formattedValue = new StringBuilder(valueToFormat);
        if(fieldToFormat.equalsIgnoreCase("S")) {

            //format as SSN

            formattedValue = formattedValue.insert(3, '-').insert(6, '-');

        } else if(fieldToFormat.equalsIgnoreCase("T")) {

            //format as telephone number
            formattedValue = formattedValue.insert(3, '-').insert(7, '-');
        }

    return formattedValue.toString();
    }
    else {
        return null;
    }
}

/**
 * This method converts a given amount string to a US $ formatted amount.
 * 
 * @param amountToFormat
 * @return
 */
public String formatAmount(String amountToFormat) {

    try {
        if(amountToFormat!=null && amountToFormat.length() > 0) {
            Locale locale = new Locale("en", "US");
            NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
            return formatter.format(Double.parseDouble(amountToFormat));
        }
        else {
            return null;
        }

    } catch (NumberFormatException nfe) {
        nfe.printStackTrace();
    } catch (IllegalArgumentException iae) {
        iae.printStackTrace();
    }

    return null;

}

Now the issue is:

  1. There are multiple pojo classes (TempAssist, SuppNutrition, ChildCare etc) which has the field related to Amount and SSN / Telephone number

  2. When we get those fields from database, firstly, the unformatted data is set in the corresponding setters and then in the UI layer, we get value through getter() and apply the above 2 functions to it and then finally respond to the client in JSON format.

Its not a clean solution as set happens twice and the code is literally bloated with GET and SET's.

What I am looking for:

An Annotation (for instance, @Format(type="ssn") which I can apply on POJO fields which will ensure that whichever fields are annotated will have SSN updated with hyphen.

This is a web application which does not use Spring framework so any suggestions on Spring cannot be implemented.

È stato utile?

Soluzione

Create a class extending JsonSerializer and then on your getter use the @JsonSerialize(using=MySerializer.class) annotation

One of the serializer could be something like:

public class MySerializer extends JsonSerializer<String> {

  @Override
  public void serialize( String value
                       , JsonGenerator jgen
                       , SerializerProvider provider) throws IOException
                                                           , JsonProcessingException {
    jgen.writeString(MyUtilsClass.formatWithHyphen(value) );
  }

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top