zoukankan      html  css  js  c++  java
  • spring webservice 开发demo (实现基本的CRUD 数据库采用H2)

    在实现过程中,遇到两个问题:

    1: schema 写错:  

    错误: http://myschool.com/schemas/st

    正确: http://myschool.com/st/schemas    ---- schemas必须结尾才是schema  这个决定了Namspace

    2: Response 对 server 端有 response 返回 需要通过采用jaxb实现

    配置如下:

    package school.info.pojo;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.springframework.stereotype.Component;
    
    @Component
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class FindByIdResponse {
    
        private Student student;
    
        public Student getStudent() {
            return student;
        }
    
        public void setStudent(Student student) {
            this.student = student;
        }
        
        
    }
    package school.info.endpoint;
    
    
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import org.jdom2.Element;
    import org.jdom2.JDOMException;
    import org.jdom2.Namespace;
    import org.jdom2.filter.Filters;
    import org.jdom2.xpath.XPathExpression;
    import org.jdom2.xpath.XPathFactory;
    import org.springframework.ws.server.endpoint.annotation.Endpoint;
    import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
    import org.springframework.ws.server.endpoint.annotation.RequestPayload;
    import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
    
    import school.info.pojo.FindAllStudentsResponse;
    import school.info.pojo.FindByIdResponse;
    import school.info.pojo.Student;
    import school.info.service.IStudentService;
    import school.info.service.StudentService;
    
    @Endpoint 
    public class StudentInfoEndpoint {
    
        private static final String namespace_schema="http://myschool.com/st/schemas";
        
        
        private IStudentService service=new StudentService();;
        
        private XPathExpression<Element> ageExpression;
        private XPathExpression<Element> nameExpression;
        private XPathExpression<Element> passwordExpression;
        private XPathExpression<Element> studentIdExpression;
        private XPathExpression<Element> levelExpression;
        
        public StudentInfoEndpoint() throws JDOMException{
            Namespace namespace=Namespace.getNamespace("st",namespace_schema);
            XPathFactory xpathFactory = XPathFactory.instance();
            studentIdExpression = xpathFactory.compile("//st:StudentId",Filters.element(),null,namespace);
            nameExpression = xpathFactory.compile("//st:Name",Filters.element(),null,namespace);
            passwordExpression = xpathFactory.compile("//st:Password",Filters.element(),null,namespace);
            ageExpression = xpathFactory.compile("//st:Age",Filters.element(),null,namespace);
            levelExpression = xpathFactory.compile("//st:Level",Filters.element(),null,namespace);
          }
        
        @PayloadRoot(namespace = namespace_schema, localPart = "RegisterRequest")
        public void handleRegisterRequest(@RequestPayload Element registerRequest){
            String name=nameExpression.evaluate(registerRequest).get(0).getValue();
            String password=passwordExpression.evaluate(registerRequest).get(0).getValue();
            String age_str=ageExpression.evaluate(registerRequest).get(0).getValue();
            long age=Long.parseLong(age_str);
            String level=levelExpression.evaluate(registerRequest).get(0).getValue();
            System.out.println(name+":"+password+":"+age+":"+level);
            service.register(name, password, age, level);
        }
        @PayloadRoot(namespace = namespace_schema, localPart = "UpdateRequest")
        public void handleUpdateRequest(@RequestPayload Element updateRequest){
            long studentId=Long.parseLong(studentIdExpression.evaluate(updateRequest).get(0).getValue());
            String name=nameExpression.evaluate(updateRequest).get(0).getValue();
            String password=passwordExpression.evaluate(updateRequest).get(0).getValue();
            String age_str=ageExpression.evaluate(updateRequest).get(0).getValue();
            long age=Long.parseLong(age_str);
            String level=levelExpression.evaluate(updateRequest).get(0).getValue();
            service.update(name, password, age, level,studentId);
        }
        @PayloadRoot(namespace = namespace_schema, localPart = "DeleteRequest")
        public void handleDeleteRequest(@RequestPayload Element deleteRequest){
            long studentId=Long.parseLong(studentIdExpression.evaluate(deleteRequest).get(0).getValue());
            service.delete(studentId);
        }
        @PayloadRoot(namespace = namespace_schema, localPart = "FindByIdRequest")
        @ResponsePayload
        public FindByIdResponse handleFindByIdRequest(@RequestPayload Element findByIdRequest){
            long studentId=Long.parseLong(studentIdExpression.evaluate(findByIdRequest).get(0).getValue());
            Student stu=service.findById(studentId);
            FindByIdResponse response=new FindByIdResponse();
            response.setStudent(stu);
            return response;
        }
        @PayloadRoot(namespace = namespace_schema, localPart = "FindAllStudentsRequest")
        @ResponsePayload
        public FindAllStudentsResponse handleFindAllStudentsRequest(@RequestPayload Element FindAllStudentsRequest){
            //long studentId=Long.parseLong(studentIdExpression.evaluate(FindAllStudentsRequest).get(0).getValue());
            List list=service.findAllStudents();
            List<Student> students=new ArrayList<Student>();
            Iterator ir=list.iterator();
            while(ir.hasNext()){
                Object[] tuple=(Object[])ir.next();
                Student s=new Student();
                s.setStudentId(Long.parseLong(tuple[0].toString()));
                s.setName(tuple[1].toString());
                s.setLevel(tuple[2].toString());
                s.setAge(Long.parseLong(tuple[3].toString()));
                s.setPassword(tuple[4].toString());
                students.add(s);
            }
            FindAllStudentsResponse all=new FindAllStudentsResponse();
            all.setList(students);
            return all;
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions xmlns:st="http://myschool.com/st/schemas" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://myschool.com/stu/definitions" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://myschool.com/st/schemas">
         <wsdl:types>
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:st="http://myschool.com/st/schemas"
            elementFormDefault="qualified"
            targetNamespace="http://myschool.com/st/schemas">
        <xs:element name="RegisterRequest">
            <xs:complexType>
                <xs:all>
                <xs:element name="Age" type="xs:long"/>
                <xs:element name="Name" type="xs:string"/>
                <xs:element name="Password" type="xs:string"/>  
                <xs:element name="Level" type="xs:string"/> 
                </xs:all>
            </xs:complexType>
        </xs:element>
        <xs:element name="UpdateRequest">
            <xs:complexType>
                <xs:all>
                    <xs:element name="Student" type="st:StudentType"/>
                </xs:all>
            </xs:complexType>
        </xs:element>
         <xs:element name="DeleteRequest">
            <xs:complexType>
                <xs:all>
                    <xs:element name="StudentId" type="xs:long"/>
                </xs:all>
            </xs:complexType>
        </xs:element>
        <xs:element name="FindByIdRequest">
            <xs:complexType>
                <xs:all>
                    <xs:element name="StudentId" type="xs:long"/>
                </xs:all>
            </xs:complexType>
        </xs:element>
        <xs:element name="FindByIdResponse">
            <xs:complexType>
                <xs:all>
                    <xs:element name="Student" type="st:StudentType"/>
                </xs:all>
            </xs:complexType>
        </xs:element>
        <xs:element name="FindAllStudentsRequest">
        </xs:element>
        <xs:element name="FindAllStudentsResponse">
            <xs:complexType>
                <xs:all>
                    <xs:element name="list" type="st:StudentTypeList"></xs:element>
                </xs:all>
            </xs:complexType>
        </xs:element>
            <xs:complexType name="StudentTypeList">
            <xs:sequence>
                <xs:element name="Student" type="st:StudentType" maxOccurs="unbounded"></xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="StudentType">
            <xs:sequence>
                 <xs:element name="StudentId" type="xs:long"/>   
                <xs:element name="Age" type="xs:long"/>
                <xs:element name="Name" type="xs:string"/>
                <xs:element name="Password" type="xs:string"/>  
                <xs:element name="Level" type="xs:string"/> 
            </xs:sequence>                                                                   
        </xs:complexType>
    </xs:schema>
        </wsdl:types>
        <wsdl:message name="DeleteRequest">
            <wsdl:part element="st:DeleteRequest" name="DeleteRequest"></wsdl:part>
        </wsdl:message>
        <wsdl:message name="RegisterRequest">
            <wsdl:part element="st:RegisterRequest" name="RegisterRequest"></wsdl:part>
        </wsdl:message>
        <wsdl:message name="FindByIdRequest">
            <wsdl:part element="st:FindByIdRequest" name="FindByIdRequest"></wsdl:part>
        </wsdl:message>
        <wsdl:message name="UpdateRequest">
            <wsdl:part element="st:UpdateRequest" name="UpdateRequest"></wsdl:part>
        </wsdl:message>
        <!-- added -->
        <wsdl:message name="FindAllStudentsRequest">
            <wsdl:part element="st:FindAllStudentsRequest" name="FindAllStudentsRequest"></wsdl:part>
        </wsdl:message>
        <wsdl:message name="FindByIdResponse">
            <wsdl:part element="st:FindByIdResponse" name="FindByIdResponse"></wsdl:part>
        </wsdl:message>
        <wsdl:message name="FindAllStudentsResponse">
            <wsdl:part element="st:FindAllStudentsResponse" name="FindAllStudentsResponse"></wsdl:part>
        </wsdl:message>
        <!-- added -->
        <wsdl:portType name="StudentService">
            <wsdl:operation name="Delete">
                <wsdl:input message="st:DeleteRequest" name="DeleteRequest"></wsdl:input>
            </wsdl:operation>
            <wsdl:operation name="Register">
                <wsdl:input message="st:RegisterRequest" name="RegisterRequest"></wsdl:input>
            </wsdl:operation>
            <wsdl:operation name="FindById">
                <wsdl:input message="st:FindByIdRequest" name="FindByIdRequest"></wsdl:input>
                <wsdl:output message="st:FindByIdResponse" name="FindByIdResponse"></wsdl:output>
            </wsdl:operation>
            <wsdl:operation name="Update">
                <wsdl:input message="st:UpdateRequest" name="UpdateRequest"></wsdl:input>
            </wsdl:operation>
            <!-- added -->
            <wsdl:operation name="FindAllStudentsRequest">
                <wsdl:input message="st:FindAllStudentsRequest" name="FindAllStudentsRequest"></wsdl:input>
                <wsdl:output message="st:FindAllStudentsResponse" name="FindAllStudentsResponse"></wsdl:output>
            </wsdl:operation>
            <!-- added -->
        </wsdl:portType>
        <wsdl:binding name="StudentServiceSoap11" type="st:StudentService">
            <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
            <wsdl:operation name="Delete">
                <soap:operation soapAction=""/>
                <wsdl:input name="DeleteRequest">
                <soap:body use="literal"/>
                </wsdl:input>
            </wsdl:operation>
            <wsdl:operation name="Register">
                <soap:operation soapAction=""/>
                <wsdl:input name="RegisterRequest">
                <soap:body use="literal"/>
                </wsdl:input>
            </wsdl:operation>
            <wsdl:operation name="FindById">
                    <soap:operation soapAction=""/>
                    <wsdl:input name="FindByIdRequest">
                    <soap:body use="literal"/>
                    </wsdl:input>
                    <wsdl:output name="FindByIdResponse">
                    <soap:body use="literal"/>
                    </wsdl:output>
            </wsdl:operation>
            <wsdl:operation name="Update">
                <soap:operation soapAction=""/>
                <wsdl:input name="UpdateRequest">
                <soap:body use="literal"/>
                </wsdl:input>
            </wsdl:operation>
            <!-- added -->
            <wsdl:operation name="FindAllStudentsRequest">
                    <soap:operation soapAction=""/>
                    <wsdl:input name="FindAllStudentsRequest">
                    <soap:body use="literal"/>
                    </wsdl:input>
                    <wsdl:output name="FindAllStudentsResponse">
                    <soap:body use="literal"/>
                    </wsdl:output>
            </wsdl:operation>
            <!-- added -->
        </wsdl:binding>
        <wsdl:service name="StudentServiceService">
                <wsdl:port binding="st:StudentServiceSoap11" name="StudentServiceSoap11">
                <soap:address location="http://localhost:8080/StudentInformationTool/"/>
                </wsdl:port>
        </wsdl:service>
    </wsdl:definitions>

    我的实现代码:https://github.com/IamGabrielWu/SpringWebService

  • 相关阅读:
    CentOS 6 安装 python and pip
    Qt for Windows:Qt 5.4.0 MinGW 静态编译版本制作 (转)
    Vim 扩展工具 vim-ide (转)
    centos yum 完全卸载依赖
    Linux修改Shell命令提示符及颜色
    tmux 命令
    网络分析shell脚本(实时流量+连接统计)
    ICMP:Internet控制报文协议
    读《分布式一致性原理》zookeeper运维
    同网段和不同网段设备通信原理详解
  • 原文地址:https://www.cnblogs.com/IamThat/p/4637603.html
Copyright © 2011-2022 走看看