0

I am working on the migration of a legacy API to microservice. The legacy API is encoding a java object into a base64 String. The following code is used:

public String serialize() throws Exception {
    String mementoXml = null;
    Document document = DocumentHelper.createDocument();
    Element dictionary = document.addElement("Dictionary");
    dictionary.add(this.addChildren());
    StringWriter sw = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setIndent(" ");
    format.setTrimText(false);
    XMLWriter writer = new XMLWriter(sw, format);

    try {
        writer.write(document);
        writer.flush();
    }
    catch (Exception var8) {

    }
    mementoXml = sw.toString().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
    return encodeString(mementoXml);
}

public Element addChildren(){
    Element response DocumentHelper.createElement("response");
    Element el1 = response.addElement("element");
    el1.setText(this.javaObject.getValue());
    return response;
}

The method for encoding is:

public String encodeString(String in) throws EncoderException {
    CompressedBase64Impl impl = new CompressedBase64Impl();
    String output = null;
    output = impl.encodeBytes(in.getBytes());
    return output;
}

What would be the modernized way in Spring boot to encode an object into a base64String. Is there something like JSONWriter?

5
  • by "String," do you mean XML? An example of the desired output format would be helpful. Commented Nov 9, 2022 at 20:57
  • @JesseBarnum The format doesn't really matter as at the end of the day the string is going to be turned back into a java object by the client. Commented Nov 9, 2022 at 20:59
  • if the format doesn't matter, then is it necessary to make it a String at all? Does it need to be BASE64 encoded? I'd say just use an ObjectOutputStream and serialize the Java object to a byte[] array. Commented Nov 9, 2022 at 21:04
  • @JesseBarnum why do you think this idea was applied to the ccbs legacy code in the first place?. We were not able to serialize jaba objects into strings easily 10 years ago? Commented Nov 9, 2022 at 21:10
  • @JesseBarnum I just mean legacy code Commented Nov 9, 2022 at 21:19

2 Answers 2

1

This writes to an ObjectOutputStream, which then writes to a Base64 encoding converter, which then writes to a byte array, which is then converted to a String.

public class EncodeToBase64 {
    public static void main(String[] args) throws Exception {
        List<?> someObjectToSerialize = new ArrayList<>();
        System.out.println( serializeToBase64( someObjectToSerialize ) );
    }

    private static String serializeToBase64( Object someObjectToSerialize ) throws IOException {
        try( final ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ObjectOutputStream objectOutputStream = new ObjectOutputStream( Base64.getEncoder().wrap( baos ) ) ) {
            objectOutputStream.writeObject( someObjectToSerialize );
            return baos.toString();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
String encodedString =  Base64.getEncoder().encodeToString(originalInput.getBytes());

This way you can convert to a base 64 encoded string. Base64 comes with java.util package.

2 Comments

How do you get there from a object?
Here originalInput is the Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.