2019-06-03 webservice 使用方法举例

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

}

//实现类

@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 服务发布

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/*");//发布服务名称

    }

@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());//绑定要发布的服务

        endpoint.publish("/updateAppAuthorServices");//显示要发布的名称

        return endpoint;

}

@Bean

    public Endpoint endpointRoleMenu() {

EndpointImpl endpoint=new EndpointImpl(springBus(), updateAppAuthorRelServices());//绑定要发布的服务

        endpoint.publish("/updateAppAuthorRelServices");//显示要发布的名称

        return endpoint;

}

@Bean

    public Endpoint endpointUser() {

EndpointImpl endpoint=new EndpointImpl(springBus(), updateAppAcctSoap());//绑定要发布的服务

        endpoint.publish("/updateAppAcctSoap");//显示要发布的名称

        return endpoint;

}

@Bean

    public Endpoint endpointUserRole() {

EndpointImpl endpoint=new EndpointImpl(springBus(), updateAppAcctAuthorServices());//绑定要发布的服务

        endpoint.publish("/updateAppAcctAuthorServices");//显示要发布的名称

        return endpoint;

}

}

转载于:https://www.jianshu.com/p/9e9af73396f1

猜你喜欢

转载自blog.csdn.net/weixin_33720078/article/details/91107936