import com.xxx.myauthority.bean.baseBean.Header;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/*角色類變更請求xml對應的bean*/
// xml 文件的根元素 將Java類或枚舉類型映射到XML元素(必須使用 指定xml跟元素)
@XmlRootElement(name ="REQUEST")
@XmlAccessorType(XmlAccessType.FIELD)
public class RoleChangeRequest {
@XmlElement(name ="HEAD")
private Headerhead;
@XmlElement(name ="BODY")
private RoleChangeRequestBodybody;
public Header getHead() {
return head;
}
public void setHead(Header head) {
this.head = head;
}
public RoleChangeRequestBody getBody() {
return body;
}
public void setBody(RoleChangeRequestBody body) {
this.body = body;
}
}
//body 中帶list 的 方式 舉例
import javax.xml.bind.annotation.*;
import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD)
public class RoleChangeRequestBody {
@XmlElement(name ="OPERATORID")
private StringOperatorId;
@XmlElement(name ="AUTHORTYPE")
private StringauthorType;
@XmlElementWrapper(name="AUTHORLIST")
@XmlElement(name ="AUTHORINFO")
private ListAuthorList;
public String getOperatorId() {
return OperatorId;
}
public void setOperatorId(String operatorId) {
OperatorId = operatorId;
}
public String getAuthorType() {
return authorType;
}
public void setAuthorType(String authorType) {
this.authorType = authorType;
}
public List getAuthorList() {
return AuthorList;
}
public void setAuthorList(List authorList) {
AuthorList = authorList;
}
}
// 接口
import org.springframework.transaction.annotation.Transactional;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
@Transactional
public interface UpdateAppAuthorServices {
@WebMethod
? ? String getRequestInfo(@WebParam(name ="requestInfo") String requestInfo);
}
//實現(xiàn)類
@WebService(targetNamespace="http://service.myauthority.zbiti.com/",endpointInterface ="com.zbiti.myauthority.service.UpdateAppAuthorServices")
public class UpdateAppAuthorServicesImplimplements UpdateAppAuthorServices {
Loggerlogger = LoggerFactory.getLogger(getClass());
@Autowired
? ? private SysUserRoleMapperauthorityMapper;
@Autowired
? ? private SysRoleMappermapper;
@Override
? ? public String getRequestInfo(String requestInfo) {
logger.info(requestInfo);
JAXBContext context =null;
RoleChangeResponse response = getRoleChangeResponse();
List list =new ArrayList<>();
try {
//讀xml 進入RoleChangeRequest
? context = JAXBContext.newInstance(RoleChangeRequest.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
RoleChangeRequest roleChangeRequest = (RoleChangeRequest) unmarshaller.unmarshal(new StringReader(requestInfo));
}catch (Exception e) {
logger.error("解析xml 到bean 出錯", e);
list.add(new ResponseAuthorInfo("4","解析xml文件出錯" + e.getMessage(),null,null,null));
}
response.getRbody().setResponseAuthorInfoList(list);
String xml1 = MyUtils.object2Xml(response);
return xml1;
}
//對象到xml
public static String object2Xml(Object object){
try
? ? {
StringWriter writer =new StringWriter();
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);// 格式化輸出
? ? ? ? marshal.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");// 編碼格式,默認為utf-8
? ? ? ? marshal.setProperty(Marshaller.JAXB_FRAGMENT,false);// 是否省略xml頭信息
? ? ? ? marshal.setProperty("jaxb.encoding","utf-8");
marshal.marshal(object,writer);
return new String(writer.getBuffer());
}catch (Exception e) { e.printStackTrace();return null;}
}
// webservice 服務發(fā)布
import com.xxx.myauthority.service.*;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Bean
? ? public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/services/*");//發(fā)布服務名稱
? ? }
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus()
{
return? new SpringBus();
}
@Bean
? ? public UpdateAppAuthorServices updateAppAuthorServices()
{
return? new UpdateAppAuthorServicesImpl();
}
@Bean
? ? public UpdateAppAuthorRelServices updateAppAuthorRelServices(){
return? new UpdateAppAuthorRelServicesImpl();
}
@Bean
? ? public UpdateAppAcctAuthorServices updateAppAcctAuthorServices(){
return? new UpdateAppAcctAuthorServicesImpl();
}
@Bean
? ? public UpdateAppAcctSoap updateAppAcctSoap(){
return? new UpdateAppAcctSoapImpl();
}
@Bean
? ? public Endpoint endpointRole() {
EndpointImpl endpoint=new EndpointImpl(springBus(), updateAppAuthorServices());//綁定要發(fā)布的服務
? ? ? ? endpoint.publish("/updateAppAuthorServices");//顯示要發(fā)布的名稱
? ? ? ? return endpoint;
}
@Bean
? ? public Endpoint endpointRoleMenu() {
EndpointImpl endpoint=new EndpointImpl(springBus(), updateAppAuthorRelServices());//綁定要發(fā)布的服務
? ? ? ? endpoint.publish("/updateAppAuthorRelServices");//顯示要發(fā)布的名稱
? ? ? ? return endpoint;
}
@Bean
? ? public Endpoint endpointUser() {
EndpointImpl endpoint=new EndpointImpl(springBus(), updateAppAcctSoap());//綁定要發(fā)布的服務
? ? ? ? endpoint.publish("/updateAppAcctSoap");//顯示要發(fā)布的名稱
? ? ? ? return endpoint;
}
@Bean
? ? public Endpoint endpointUserRole() {
EndpointImpl endpoint=new EndpointImpl(springBus(), updateAppAcctAuthorServices());//綁定要發(fā)布的服務
? ? ? ? endpoint.publish("/updateAppAcctAuthorServices");//顯示要發(fā)布的名稱
? ? ? ? return endpoint;
}
}