Jaxb Marshals the same XML Message

▼魔方 西西 提交于 2020-01-24 21:47:05

问题


I am using Jaxb for marshalling and unmarshalling objects. This is the code of my custom class for the purpose.

import test.org.swt.SwitchMsg;
import test.org.swt.TrxMsg;
import org.springframework.context.annotation.Configuration;
import org.springframework.xml.transform.StringSource;

import javax.annotation.PostConstruct;
import javax.xml.bind.*;
import java.io.StringWriter;

@Configuration
public class JaxbConfig {

    private JAXBContext jaxbContext;

    @PostConstruct
    private void init(){
        try {
            jaxbContext = JAXBContext.newInstance(
                    SwitchMsg.class,
                    TrxMsg.class,
            );

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public <T> T unmarshal(String xmlString, Class<T> clazz) {
        try {
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            JAXBElement<T> element = unmarshaller.unmarshal(new StringSource(xmlString), clazz);

            return element.getValue();

        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    public String marshal(Object object) {
        try {
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

            StringWriter stringWriter = new StringWriter();
            marshaller.marshal(object, stringWriter);
            return stringWriter.toString();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

This is my code for unmarshalling and marshalling. As you can see, I use the unmarshall() method to create a TrxMsg object.And I use the data to invoke another method which returns a TrxMsg object. But when I marshall it again, I get my old object back. Not the new one. I hope my code comments will clarify more. I need to know why this is happening and how to solve it. Thanks in advance.

private void processTransaction(ChannelHandlerContext ctx, String payload){

        String logMsg = payload;

        log.debug("Request received: \n{}", logMsg);

        //(1) 
        //I get a TrxMsg object by unmarshalling - Works perfectly fine
        TrxMsg trxMsg = jaxbConfig.unmarshal(payload, TrxMsg.class);
        Request request = new Request();

        SwitchMsg switchMsg = trxMsg.getMsg();

        request.setMessageId(switchMsg.getMessageId());
        request.setMessage(switchMsg);

        Response response = routerService.send(request);

        //response.getResponse() also returns a TrxMsg object but I get the same object 
        //I got in (1).
       //This TrxMsg object has different values. I tested in Logs
        String res = jaxbConfig.marshal(response.getResponse());

        log.info("Sending response: \n{}", res);
        final ByteBuf responseBuf = Unpooled.wrappedBuffer(res.getBytes(StandardCharsets.UTF_8));

        ChannelFuture future = ctx.writeAndFlush(responseBuf);
        future.addListener(ChannelFutureListener.CLOSE);
    }

来源:https://stackoverflow.com/questions/58470012/jaxb-marshals-the-same-xml-message

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