REST Service 开发 - JAXRS 基础
【摘要】本文介绍 JAXRS 常用语法与语义,案例则使用 Maven 多模块构建,集成 spring boot,
并使用 swagger 可视化工具测试服务。最终,给出同时支持 JSON 和 XML 的服务配置。
1、准备项目
1. 下载 CXF 案例
计算机学习看文档,找教程固然重要。运行官方案例 + 模仿编程才是最佳的学习方法。
编程学习总是满足“一万行定律”,即编写相关代码足够,自然理解编程的要领。
用教学学术语叫“程序性知识”,也如同学习驾驶汽车,开一万公里就学会了。
CXF 提供的案例可在官方下载获取,请下载最新二进制发行版:
http://cxf.apache.org/download.html
解压 samples 目录,作为参考
2. 创建项目
- 建立项目目录, 例如:my-samples
- 在项目中创建 maven 构建文件 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.me.test</groupId>
<artifactId>my-samples</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>My REST Service Samples</name>
<description>My REST Service Samples</description>
<url>http://blog.youkuaiyun.com/pmlpml</url>
<properties>
<!-- don't deploy the samples, kind of pointless -->
<maven.deploy.skip>true</maven.deploy.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>1.3.6.RELEASE</spring.boot.version>
<spring.cloud.version>1.1.3.RELEASE</spring.cloud.version>
<cxf.version>3.1.10</cxf.version>
<feign.version>8.18.0</feign.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j