简单的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)
简单的cxf-rs的更多相关文章
- WebService 之CXF(rs)之HelloWorld
1.建立mavenjava项目 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- webservice的简单使用,cxf框架的的使用
Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...
- 使用CXF做简单的WebService例子
使用Maven搭建项目测试简单的CXF实例 Server: pom.xml: <!-- begin CXF Server --> <dependency> <groupI ...
- WebService--使用 CXF 开发 REST 服务
现在您已经学会了如何使用 CXF 开发基于 SOAP 的 Web 服务,也领略了 Spring + CXF 这个强大的组合,如果您错过了这精彩的一幕,请回头看看这篇吧: Web Service 那点事 ...
- CXF 开发 REST 服务
今天我们将视角集中在 REST 上,它是继 SOAP 以后,另一种广泛使用的 Web 服务.与 SOAP 不同,REST 并没有 WSDL 的概念,也没有叫做"信封"的东西,因为 ...
- SSH集成cxf 发布restful webservice
首先讲一下什么是restful webservice ,这个问题网上一搜有很多博文去长篇大论的介绍它,但是最后你看完了也会觉得云里雾里的,所以我在这里简单的讲一下我理解的rest webservice ...
- ajax跨域访问 java controller 和 cxf(webservice) 配置方式(CORS)
1. controller跨域访问,配置方式 重点在这里: <mvc:cors> <mvc:mapping path="/*" allowed-origins=& ...
- [转贴]JAVA :CXF 简介
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF.CXF 继 ...
- (六)cxf处理一些Map等复杂类型
前面讲的一些都是简单类型,cxf都支持.但是有些复杂类型,cxf是不支持,比如常用的Map类型: 下面我们在前面的实例基础上在加一个方法,比如我们现在有个需求,获取所有用用户以及对应的每个用户所有角色 ...
- webService之helloword(java)rs
webservice之rs(helloworld) 1.pom.xml文件 <dependencies> <!-- 使用CXF RS开发 --> <dependency& ...
随机推荐
- textbox显示定位到最后一行(最新一行)
this.textBox1.Select(this.txtMsgInfo.TextLength, 0); this.textBox1.ScrollToCaret();
- 逻辑斯蒂回归VS决策树VS随机森林
LR 与SVM 不同 1.logistic regression适合需要得到一个分类概率的场景,SVM则没有分类概率 2.LR其实同样可以使用kernel,但是LR没有support vector在计 ...
- HTML鼠标悬浮显示隐藏 JS方法
CSS样式表: @charset "utf-8"; /* CSS Document */ .a { width:80px; height:40px; top:200px; left ...
- 读《asp.net MVC4开发指南(黄保翕编著)》笔记
在刚刚过去的中秋节中,利用了两天的碎片时间把黄保翕编著的<asp.net MVC4 开发指南>看了遍,笔记如下,欢饮在开发MVC的同学一起来探讨: 1.社区 2.开源程序 3.易测试性 4 ...
- (mac环境)Appium安装了client包,但是提示no module named appium
背景 mac环境,使用pip install Appium-Python-Client已经安装了client包 问题 import appium,提示no module named appium ...
- JPA调用函数
criteriaBuilder.function("udf_get_cc_userright", Integer.class, criteriaBuilder.literal(se ...
- 结对编程--fault,error,failure
结对编程对象:叶小娟 对方博客地址:http://www.cnblogs.com/yxj63/ 双方贡献比例:1:1 结对照片: 结对题目:输入一定个数的数字,对其排序后输出最大值. 1 pack ...
- TensorFlow—CNN—CIFAR数据集分类
- 转)Ubuntu14安装wireshark进行抓包
转自:http://jingyan.baidu.com/article/c74d60009d992f0f6a595de6.html 背景: ubuntu14.04/64位 为了抓包和分析包 安装过程: ...
- 编辑距离12 · Edit Distance12
[抄题]: 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 [思维问题]: [一句话思路]: ...