春の統合開発残りCXFスタイルのWebサービス・インタフェース(クライアント&サーバー)

春のWebサービスクライアントとサーバーを開発CXFを統合する方法前の記事の文書は、実際には、開発の本来の方法に比べて、すでに最適化をたくさん持っています。ヘルプ私たちはより簡潔かつ効率的な物流サービス、フロントサービスであるためには、非常に人気の開発仕様として、スタイル開発を休みます。

クライアント

  1. 依存の追加
    すべてJAXWS前に、今のプログラムはW Rになり、意味の残りがあります。
<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>3.3.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.3.5</version>
      <scope>compile</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-client -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-client</artifactId>
      <version>3.3.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-extension-providers -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-extension-providers</artifactId>
      <version>3.3.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
    <dependency>
      <groupId>org.codehaus.jettison</groupId>
      <artifactId>jettison</artifactId>
      <version>1.4.0</version>
    </dependency>
  1. web.xml構成
    とこれ以上の変更の前に
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>cXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>cXFServlet</servlet-name>
    <url-pattern>/webService/*</url-pattern>
  </servlet-mapping>
   <!--  2.配置spring容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    <!--    3.监听器-->
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  1. 書き込みエンティティクラス
    @XmlRootElement注釈指定したオブジェクトは、XMLの後に/ jsonのルートにシリアライズされます。
@XmlRootElement(name="student")
public class Student {

    private Integer id;
    private String name;
    private String gender;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}

  1. サービス・インターフェースと実装クラス
    IStudnetServiceインターフェース、前記@Pathは、サービスインターフェイスを対応する現在のアクセスパスを表し;サーバ支持部@Consumesのデータ・タイプを受信するステップと、@Producesサーバが返さ指定されたデータタイプをサポート
public interface IStudnetService {

    /**
     * post对应的是insert操作
     * get对应的是select操作
     * put对应的是update操作
     * delete对应的是delete操作
     *
     * @param student
     */
    @POST
    @Path("/student")
    @Consumes({"application/xml","application/json"})
    public void addStudent(Student student);

    @PUT
    @Path("/student")
    @Consumes({"application/xml","application/json"})
    public void saveStudent(Student student);

    @GET
    @Path("/student/{id}")
    @Consumes({"application/xml","application/json"})
    @Produces({"application/xml","application/json"})
    public Student getStudentById(@PathParam("id") Integer id);

    @GET
    @Path("/student")
    @Produces({"application/xml","application/json"})
    public List<Student> getStudent();

    @DELETE
    @Path("/student/{id}")
    @Consumes({"application/xml","application/json"})
    public void deleteStudent(@PathParam("id") Integer id);
}

StudentService実装クラス
ここでは、クライアントがサービスを呼び出すときに、サーバーがログを印刷し、シミュレーションスタイルのCRUDを休みます。

public class StudentService implements IStudnetService {
    @Override
    public void addStudent(Student student) {
        System.out.println(student.getName()+"学生信息添加成功!");
    }

    @Override
    public void saveStudent(Student student) {
        System.out.println(student.getName()+"学生信息修改成功!");
    }

    @Override
    public Student getStudentById(Integer id) {
        Student student = new Student();
        student.setId(id);
        student.setAge(13);
        student.setGender("男");
        student.setName("william");
        System.out.println("id为"+id+"学生信息查询成功!");
        return student;
    }

    @Override
    public List<Student> getStudent() {
        Student student1 = new Student();
        student1.setId(1);
        student1.setAge(13);
        student1.setGender("男");
        student1.setName("william");
        Student student2 = new Student();
        student2.setId(2);
        student2.setAge(12);
        student2.setGender("女");
        student2.setName("elaine");

        List<Student> studentList = new ArrayList<Student>();
        studentList.add(student1);
        studentList.add(student2);
        System.out.println("成功查询所有学生信息成功!");
        return studentList;
    }

    @Override
    public void deleteStudent(Integer id) {
        System.out.println("id为"+id+"的学生信息删除成功!");
    }
}

  1. 設定applicationContext.xmlを
    構成サービスの道はほとんど変化、ノートjaxrsラベルを取る必要があり、対応する名前空間があります。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd ">

        <jaxrs:server address="/studentService">
            <jaxrs:serviceBeans>
                <bean class="com.wuwl.service.impl.StudentService"></bean>
            </jaxrs:serviceBeans>
        </jaxrs:server>

</beans>
  1. スタートTomcatサービスが
    正常に起動した後、我々はブラウザを介してアクセス:http://localhost:8081/webService/studentService/student/123ページ上に表示されます。
<?xml version="1.0" encoding="UTF-8" standalone="true"?>

<student>

<age>13</age>

<gender></gender>

<id>123</id>

<name>william</name>

</student>

クライアント
書くクライアントは、より単純明快です。
7.その依存関係の導入
エンティティ・クラスが導入された8
ここでは、インポートする必要があるStudentエンティティクラスを。
9.クライアント書き込みテストコード
Webクライアントオブジェクトを介して呼サービスは、異なる要求タイプを送信し、自動的に対応するサービスメソッドと一致

public class Client {

    @Test
    public void testPost(){
        Student st1 = new Student();
        st1.setId(10);
        st1.setName("jack");
        st1.setGender("男");
        st1.setAge(15);
        //通过webClient对象远程调用服务
        WebClient.create("http://localhost:8081/webService/studentService/student").type(MediaType.APPLICATION_JSON).post(st1);
    }
    @Test
    public void testDelete(){

        WebClient.create("http://localhost:8081/webService/studentService/student/12").type(MediaType.APPLICATION_JSON).delete();
    }
    @Test
    public void testPut(){
        Student st1 = new Student();
        st1.setId(10);
        st1.setName("marry");
        st1.setGender("/女");
        st1.setAge(15);
        //通过webClient对象远程调用服务
        WebClient.create("http://localhost:8081/webService/studentService/student").type(MediaType.APPLICATION_JSON).put(st1);
    }
    @Test
    public void testGetById(){

        Student student = WebClient.create("http://localhost:8081/webService/studentService/student/12").accept(MediaType.APPLICATION_JSON).get(Student.class);
        System.out.println(student);
    }
    @Test
    public void testGet(){

        List<Student> studentList = (List<Student>) WebClient.create("http://localhost:8081/webService/studentService/student")
                .accept(MediaType.APPLICATION_JSON).getCollection(Student.class);
        System.out.println(studentList);
    }
}


GETリクエストに加えて、クライアントは任意の出力を見ることができない、値を返しませんでしたが、サーバーが対応する要求レコードを表示することができますので、私たちは、この方法をテストし、トップダウンの実行ユニットの電源を入れます。

jack学生信息添加成功!
id为12的学生信息删除成功!
marry学生信息修改成功!

次に、我々はtestGetByIdメソッドをテストしました。
クライアントの出力:

Student{id=12, name='william', gender='男', age=13}

サーバーの出力:

id为12学生信息查询成功!

最後に、テストtestGet方法。
クライアントの出力:

[Student{id=1, name='william', gender='男', age=13}, Student{id=2, name='elaine', gender='女', age=12}]

サーバーの出力:

成功查询所有学生信息成功!
公開された98元の記事 ウォン称賛13 ビュー20000 +

おすすめ

転載: blog.csdn.net/qq_41885819/article/details/104878059