• 简单的cxf-rs


    整体结构

    │  pom.xml
    │
    └─src
        ├─main
        │  ├─java
        │  │  └─cn
        │  │      └─zno
        │  │              HelloWorld.java
        │  │              InputBean.java
        │  │
        │  └─webapp
        │      └─WEB-INF
        │              beans.xml
        │              web.xml
        │
        └─test
            └─java
                └─cn
                    └─zno
                            HelloWorldIT.java

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>cn.zno</groupId>
        <artifactId>cxf-rs</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>Simple CXF JAX-RS webapp service using spring configuration</name>
        <description>Simple CXF JAX-RS webapp service using spring configuration</description>
        <properties>
            <jackson.version>1.8.6</jackson.version>
            <test.server.port>8080</test.server.port>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxrs</artifactId>
                <version>3.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-rs-client</artifactId>
                <version>3.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-rs-service-description</artifactId>
                <version>3.0.0-milestone1</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-asl</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-jaxrs</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.1.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <source>1.7</source>
                            <target>1.7</target>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>

    web.xml

    <?xml version="1.0" encoding="utf-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
        <display-name>JAX-RS Simple Service</display-name>
        <description>JAX-RS Simple Service</description>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/beans.xml</param-value>
        </context-param>
        <listener>
            <listener-class>
                            org.springframework.web.context.ContextLoaderListener
                    </listener-class>
        </listener>
        <servlet>
            <servlet-name>CXFServlet</servlet-name>
            <servlet-class>
                            org.apache.cxf.transport.servlet.CXFServlet
                    </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>CXFServlet</servlet-name>
            <url-pattern>/a/*</url-pattern>
        </servlet-mapping>
    </web-app>

    beans.xml

    <?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:jaxrs="http://cxf.apache.org/jaxrs"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:cxf="http://cxf.apache.org/core"
    
        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/jaxrs 
                  http://cxf.apache.org/schemas/jaxrs.xsd
                  http://cxf.apache.org/core 
                  http://cxf.apache.org/schemas/core.xsd">
    
    
        <import resource="classpath:META-INF/cxf/cxf.xml" />
    
        <bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider" />
        <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
        <bean id="inInterceptors" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        <bean id="outInterceptors" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
    
        <jaxrs:server id="services" address="/b">
            <jaxrs:serviceBeans>
                <bean class="cn.zno.HelloWorld" />
            </jaxrs:serviceBeans>
            <jaxrs:providers>
                <ref bean="jsonProvider" />
                <ref bean="jaxbProvider" />
            </jaxrs:providers>
            <jaxrs:extensionMappings>
                <entry key="json" value="application/json" />
                <entry key="xml" value="application/xml" />
            </jaxrs:extensionMappings>
            <jaxrs:languageMappings>
                <entry key="en" value="en-gb" />
            </jaxrs:languageMappings>
            <jaxrs:inInterceptors>
                <ref bean="inInterceptors" />
            </jaxrs:inInterceptors>
            <jaxrs:outInterceptors>
                <ref bean="outInterceptors" />
            </jaxrs:outInterceptors>
        </jaxrs:server>
    </beans>

    HelloWorld.java

    package cn.zno;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Response;
    
    import org.apache.cxf.jaxrs.model.wadl.Description;
    import org.apache.cxf.jaxrs.model.wadl.DocTarget;
    
    @Path("/c")
    @Description(value = "Resource", target = DocTarget.RESOURCE)
    public class HelloWorld {
    
        @GET
        @Path("/echo/{input}")
        @Produces("text/plain")
        public String ping(@PathParam("input") String input) {
            return input;
        }
    
        @POST
        @Produces("application/json")
        @Consumes("application/json")
        @Path("/jsonBean")
        public Response modifyJson(InputBean input) {
            input.setVal2(input.getVal1());
            return Response.ok().entity(input).build();
        }
    }

    InputBean.java

    package cn.zno;
    
    
    public class InputBean {
        private String val1;
        private String val2;
    
        public String getVal1() {
            return val1;
        }
    
        public void setVal1(String val1) {
            this.val1 = val1;
        }
    
        public String getVal2() {
            return val2;
        }
    
        public void setVal2(String val2) {
            this.val2 = val2;
        }
    
        @Override
        public String toString() {
            return "InputBean [val1=" + val1 + ", val2=" + val2 + "]";
        }
    }

    HelloWorldIT.java

    package cn.zno;
    
    import static org.junit.Assert.assertEquals;
    
    import java.io.InputStream;
    import java.util.Collections;
    import java.util.List;
    
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    
    import org.apache.cxf.helpers.IOUtils;
    import org.apache.cxf.jaxrs.client.WebClient;
    import org.junit.Test;
    
    public class HelloWorldIT {
        private static String endpointUrl = "http://localhost:8080/cxf-rs/a/b";
    
    
        @Test
        public void testPing() throws Exception {
            WebClient client = WebClient.create(endpointUrl + "/c/echo/SierraTangoNevada");
            Response r = client.accept("text/plain").get();
            assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
            String value = IOUtils.toString((InputStream)r.getEntity());
            assertEquals("SierraTangoNevada", value);
        }
    
        @Test
        public void testJsonRoundtrip() throws Exception {
    //        List<Object> providers = new ArrayList<Object>();
    //        providers.add(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());
            
            List<org.codehaus.jackson.jaxrs.JacksonJsonProvider> provider = Collections.singletonList(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());
            
            InputBean inputBean = new InputBean();
            inputBean.setVal1("Maple");
            WebClient client = WebClient.create(endpointUrl + "/c/jsonBean", provider);
            InputBean jsonBean = client.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
                .post(inputBean,InputBean.class);
            System.out.println(jsonBean.toString());
    //        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
    //        MappingJsonFactory factory = new MappingJsonFactory();
    //        JsonParser parser = factory.createJsonParser((InputStream)r.getEntity());
    //        JsonBean output = parser.readValueAs(JsonBean.class);
    //        assertEquals("Maple", output.getVal2());
        }
    }

    ?_wadl

    开启方式:@Description 注解

    @Description(value = "Resource", target = DocTarget.RESOURCE)

    
    
  • 相关阅读:
    Ubuntu16.04下同时安装Anaconda2与Anaconda3
    ansible 常用模块
    docker 笔记 (7) 限制容器
    linux 磁盘
    docker 笔记 (6)搭建本地registry
    docker 笔记 (5)常用命令
    docker 笔记(4) Dockerfile 常用的指令
    NGINX下配置CACHE-CONTROL
    mysql二进制安装
    [Selenium] Explicit wait 方法
  • 原文地址:https://www.cnblogs.com/zno2/p/4689071.html
Copyright © 2020-2023  润新知