飞道的博客

Java restful WebService 发布与访问

253人阅读  评论(0)

1. Restful概念介绍

Restful并不是一种新技术,它只是一种设计风格,能够简化HTTP请求。

2. restful 风格的WebService的发布

<1>.新建Maven项目
新建Maven项目,在pom.xml文件中加入依赖,导入需要的包,pom.xml文件如下

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.webservice.RsWebService</groupId>
  <artifactId>RsWebService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
   <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-client</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
            <version>3.0.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.3.7</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

<2>.新建实体类

package student;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "StudentPO")
public class StudentPO {

	public StudentPO(String id, String name, String sex, int gender) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.gender = gender;
	}
	public StudentPO() {

	}
	private String id;
	private String name;
	private String sex;
	private int gender;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getGender() {
		return gender;
	}
	public void setGender(int gender) {
		this.gender = gender;
	}
	
	@Override
	public String toString() {
		return "StudentPO [id=" + id + ", name=" + name + ", sex=" + sex + ", gender=" + gender + "]";
	}


}

<2>.新建服务接口

package student;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/studentService")//访问当前服务接口对应的路径
@Produces("*/*")  // 服务器支持的返回的数据格式类型
public interface StudentService {
	
		@GET// 表示处理的请求的类型,get 对应的是查询修改操作s
		@Path("/findAllStudent")
		// 服务器支持的返回的数据格式类型
		@Produces({ "application/xml", "application/json" })
		public List<StudentPO> findAllStudent();
		
		
		@POST
		@Path("/saveStudent") // 路径; 访问当前服务接口的方法路径	
		@Consumes({ "application/xml", "application/json" })// @Consumes 服务端支持的请求的数据格式(xml、json)
		public void saveStudent(StudentPO StudentPO);

}

<3>.新建接口实现类

package student;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceImpl implements StudentService {
	
		@Override
		public List<StudentPO> findAllStudent() {
			ArrayList<StudentPO> studentPOs=new ArrayList<>();
			StudentPO studentPO1=new StudentPO();
			studentPO1.setId("001");
			studentPO1.setName("菜虚鲲");
			studentPO1.setSex("female");
			studentPO1.setGender(1);
			StudentPO studentPO2=new StudentPO();
			studentPO2.setId("002");
			studentPO2.setName("鸡太美");
			studentPO2.setSex("female");
			studentPO2.setGender(2);
			studentPOs.add(studentPO1);
			studentPOs.add(studentPO2);
			return 	studentPOs;
		}

		@Override
		public void saveStudent(StudentPO StudentPO) {
			
			System.out.println(StudentPO);
		}
	
	

}

<4>.新建发布服务的类

package util;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

import student.StudentServiceImpl;

public class PublishService {

	public static void main(String[] args) {
		
		//1.创建服务工厂
		JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
		//2.设置服务地址、
		String address="http://localhost:8001/rs";
		factory.setAddress(address);
		//3.实例化服务类、
		factory.setServiceBean(new StudentServiceImpl());
		//4.创建服务
		factory.create();
		System.out.println("发布服务成功..地址:"+ address);

	}

}

<5>.运行程序发布服务

3.客户端的访问
<1>.新建maven项目
新建Maven项目,在pom.xml文件中加入依赖,导入需要的包,pom.xml文件如下

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.WebServiceRS</groupId>
  <artifactId>WebServiceRS</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
   <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-client</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.3.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

<2>.新建实体类

package student;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="StudentPO")
public class StudentPO {
	
	@SuppressWarnings("unused")
	private StudentPO() {

	}
	
	public StudentPO(String id, String name, String sex, int gender) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "StudentPO [id=" + id + ", name=" + name + ", sex=" + sex + ", gender=" + gender + "]";
	}
	private String id;
	private String name;
	private String sex;
	private int gender;
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getGender() {
		return gender;
	}
	public void setGender(int gender) {
		this.gender = gender;
	}


}

<2>.新建测试类
新建测试类,验证客户端与服务是否能够正常通信,这里写了两个方法,一个是向服务端请求数据,一个是以post方式向服务端发送数据

package util;

import java.util.Collection;
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.jupiter.api.Test;

import student.StudentPO;

public class RsClient {
		
    @Test
    public void findAllStudent() throws Exception {
    	//查询方法
		Collection<? extends StudentPO> studentPOs=
				WebClient.create("http://localhost:8001/rs/studentService/findAllStudent/")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(StudentPO.class);
		System.out.println(studentPOs.toString());		
    }
    
    @Test
    public void saveStudent() throws Exception {
		//新增方法
		WebClient.create("http://localhost:8001/rs/studentService/saveStudent") //请求
		.type(MediaType.APPLICATION_JSON) //请求数据格式是json
		.post(new StudentPO("666","小猪","man",18));// 以post方式发送请求
    }
}

Junit运行第一个方法,向服务端请求数据

Junit运行第二个方法,以post方式向服务端发送数据


转载:https://blog.csdn.net/shisen0530/article/details/105903361
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场