webService之helloword(java)rs

Posted fjk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webService之helloword(java)rs相关的知识,希望对你有一定的参考价值。

webservice之rs(helloworld)

1.pom.xml文件

<dependencies>
  		<!-- 使用CXF RS开发 -->
  		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxrs</artifactId>
			<version>3.0.1</version>
		</dependency>
 	
 		<!-- 内置jetty web服务器 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.0.1</version>
		</dependency>

		<!-- 使用log4j日志实现  -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.12</version>
		</dependency>
		
		<!-- 在CXF扩展提供者,提供转换json接口  -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-rs-extension-providers</artifactId>
			<version>3.0.1</version>
		</dependency>

		<!-- cxf 扩展提供者 转换json 默认需求一个工具包  -->
		<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>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
	</build>

  2.创建服务接口

package com.baidu.service;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import com.baidu.domain.User;

@Path("/userService")//服务器访问资源路径
//@Produces("*/*")//  生成     方法的返回值   //指定能否生成哪种格式数据返回给客户端
public interface UserService {
	//@GET 查询 @PUT 修改 @POST 增加 @DELETE 删除
	
	@POST 
	@Path("/saveUser")
	@Consumes({"application/xml","application/json"})  //消费   方法的参数
	public void saveUser(User user);
	@DELETE
	@Path("/deleteUser")
	@Consumes({"application/xml","application/json"})
	public void deleteUser(@QueryParam(value="id")Integer id);
	@PUT
	@Path("/updateUser")
	@Consumes({"application/xml","application/json"})
	public void updateUser(User user);
	@GET
	@Path("/queryUser/{id}")
	@Consumes({"application/xml","application/json"})
	@Produces({"application/xml","application/json"})
	public User queryUser(@PathParam(value="id")Integer id);
}

 

 

@Path 服务器访问资源路径
@Produces 指定生成那种格式的额数据格式返回给客户端
@Consumes 指定接收的数据格式
@POST 指定该方法为保存(保存)
@DELETE 指定该方法为删除
@PUT 指定该方法为修改
@GET  指定该方法为查询

 

  

 

 

 

 

3.创建服务实现类

package com.baidu.service.imp;

import com.baidu.domain.User;
import com.baidu.service.UserService;

public class UserServiceImp implements UserService {
	public void saveUser(User user) {
		System.out.println("saveUser"+user);
	}
	public void deleteUser(Integer id) {
		System.out.println("deleteUser id:"+id);
	}
	public void updateUser(User user) {
		System.out.println("updateUser user:"+user);
	}
	public User queryUser(Integer id) {
		System.out.println("id");
		if(id==1){
			System.out.println(id);
			User u=new User();
			u.setId(id);
			u.setName("zhangsan");
			return u;
		}
		return null;
	}
}

  4.发布服务

package com.baidu.publish;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

import com.baidu.domain.User;
import com.baidu.service.UserService;
import com.baidu.service.imp.UserServiceImp;

public class PublishService {
	public static void main(String[] args) {
		UserService us=new UserServiceImp();
		JAXRSServerFactoryBean serverFactoryBean=new JAXRSServerFactoryBean();
		serverFactoryBean.setResourceClasses(User.class);
		serverFactoryBean.setServiceBean(us);
		serverFactoryBean.setAddress("http://localhost:8099");
		serverFactoryBean.create();
	}
}

测试项目的pom.xml文件

 <dependencies>
  		<!-- 使用CXF RS开发 -->
  		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxrs</artifactId>
			<version>3.0.1</version>
		</dependency>

		<!-- 使用log4j日志实现  -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.12</version>
		</dependency>
		
		<!-- 使用rs客户端  -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-rs-client</artifactId>
			<version>3.0.1</version>
		</dependency>
		
		<!-- 在CXF扩展提供者,提供转换json接口  -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-rs-extension-providers</artifactId>
			<version>3.0.1</version>
		</dependency>

		<!-- cxf 扩展提供者 转换json 默认需求一个工具包  -->
		<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>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
	</build>

 测试发布的服务

    public static void main(String[] args) {
		// 保存
		// User user = new User();
		// user.setId(1);
		// user.setName("zhangsan");
		// WebClient.create("http://localhost:8099/userService/saveUser")
		// .type(MediaType.APPLICATION_XML).post(user);

		// 删除
		// WebClient.create("http://localhost:8099/userService/deleteUser?id=1").type(MediaType.APPLICATION_XML).delete();
		
		// 修改
		// User user = new User();
		// user.setId(1);
		// user.setName("zhangsan");
		// WebClient.create("http://localhost:8099/userService/deleteUser").type(MediaType.APPLICATION_XML).put(user);

		// 查询  
		// User user =WebClient.create("http://localhost:8099/userService/queryUser/1")
		// .accept(MediaType.APPLICATION_XML).get(User.class);
		// System.out.println(user);

	} 

 User类

package com.baidu.domain;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="User")
public class User {
	
	private Integer Id;
	private String name;
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [Id=" + Id + ", name=" + name + "]";
	}
	
}

  

 

以上是关于webService之helloword(java)rs的主要内容,如果未能解决你的问题,请参考以下文章

webService之helloword(web)

java之spring之helloword

J2. Java程序设计之环境搭建与HelloWord

Notes 20180507 : Java程序设计之环境搭建与HelloWord

WebService -- Java 实现之 CXF (WebService 服务器端接口)

WebService_Demo