cdata xml java_SpringMVC收发带CDATA的xml报文

本文介绍了如何在Java SpringMVC应用中处理包含CDATA的XML报文。通过引入eclipselink包,创建实体类并使用特定注解,以及在controller接口中使用@ResponseBody和@RequestBody,实现了接收和发送带有CDATA的XML数据。
摘要由CSDN通过智能技术生成

首先需要引入eclipselink包:

org.eclipse.persistence

eclipselink

2.5.0

实体类包下面需要添加jaxb.properties文件:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

实体类使用如下注解:

package com.chinamobile.cmss.ehub.ssb.test.entity;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlCDATA;

@XmlRootElement(name="InterBOSS")

@XmlAccessorType(XmlAccessType.FIELD)

public class UserStateRqeMessage {

@XmlCDATA

private String SvcCont;

public String getSvcCont() {

return SvcCont;

}

public void setSvcCont(String svcCont) {

SvcCont = svcCont;

}

/*private UserStateDetail SvcCont;

public UserStateDetail getSvcCont() {

return SvcCont;

}

public void setSvcCont(UserStateDetail svcCont) {

SvcCont = svcCont;

}*/

}

controller接口使用@ResponseBody和@RequestBody

package com.chinamobile.cmss.ehub.ssb.test.controller;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Marshaller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import com.chinamobile.cmss.ehub.ssb.test.entity.UserStateRqeMessage;

import com.chinamobile.cmss.ehub.ssb.test.entity.UserStateRspMessage;

@Controller

public class UserController {

@ResponseBody

@RequestMapping(value="/users", method=RequestMethod.POST)

public UserStateRspMessage checkUserState(@RequestBody UserStateRqeMessage userStateRqeMessage) throws JAXBException{

System.out.println(userStateRqeMessage.getSvcCont());

JAXBContext jc = JAXBContext.newInstance(UserStateRspMessage.class);

UserStateRspMessage userStateRspMessage = new UserStateRspMessage();

Marshaller marshaller = jc.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(userStateRspMessage, System.out);

return userStateRspMessage;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值