Java Jaxb封送相同的XML消息

Java Jaxb封送相同的XML消息,java,xml,jaxb,Java,Xml,Jaxb,我使用Jaxb对对象进行编组和解编组。这是我的自定义类的代码 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; impo

我使用Jaxb对对象进行编组和解编组。这是我的自定义类的代码

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);
        }
    }
}
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);
    }