demo项目地址:gitee.com/Jxnuxyhsz/c…
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- jaxrs : CXF Restful webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.2.4</version>
<exclusions>
<!--The Bean Validation API is on the classpath but no implementation could be found-->
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--Google Guice 一个轻量级的依赖注入框架-->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.12.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
复制代码
domain
/**
* User实体类
*/
@Data
@AllArgsConstructor
@XmlRootElement // 对象转换成xml/json
public class User implements Serializable {
private static final long serialVersionUID = -2561117553127467595L;
// 工号
private Long userId;
// 姓名
private String userName;
// 住址
private String address;
}
复制代码
IUserService
/**
* User接口
*
*/
@Path("/users")
@Produces(value = {MediaType.APPLICATION_JSON}) // 返回json格式
public interface IUserService {
/**
* 返回指定userIdD的user信息
*
* @param userId
* @return User
*/
@GET
@Path("/{userId}")
User selectUserInfo(@PathParam("userId") Long userId);
}
复制代码
UserServiceImpl
@Named
public class UserServiceImpl implements IUserService {
@Override
public User selectUserInfo(Long userId) {
return new User(userId, "JxnuXYH...", "JiangXi");
}
}
复制代码
application.properties
server.port=8080
server.servlet.context-path=/demo
复制代码
cxf-config.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.example.demo"/>
<!-- 暴露Service -->
<jaxrs:server id="restContainer" address="/rest">
<jaxrs:serviceBeans>
<ref bean="userServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:providers> <!-- json转化 -->
<ref bean="jacksonJsonProvider" />
</jaxrs:providers>
</jaxrs:server>
</beans>
复制代码
DemoApplication
@SpringBootApplication
@ImportResource(locations = "classpath:cxf-config.xml")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public JacksonJsonProvider jacksonJsonProvider(){
return new JacksonJsonProvider();
}
}
复制代码
访问测试
ClientTest
/**
* 模拟客户端访问测试
* 使用httpClient访问
*/
public class ClientTest {
public static void main(String[] args) {
// 获取httpClient
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建请求
HttpGet httpGet = new HttpGet("http://localhost:8080/demo/services/rest/users/123");
// 响应类型
CloseableHttpResponse response = null;
InputStream inputStream = null;
try{
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
inputStream = entity.getContent();
int len = 0; // 记录读取长度
int tmp;
byte[] data = new byte[1024]; // 开辟一个空间
while ((tmp = inputStream.read()) != -1) {
data[len++] = (byte) tmp; // 保存在字节数组中
}
System.out.println("客户端访问到的内容: " + new String(data, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
// 客户端访问到的内容: {"userId":123,"userName":"JxnuXYH...","address":"JiangXi"}
复制代码