欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jersey webservice

程序员文章站 2022-07-14 12:25:07
...
在web.xml里加入以下代码
  <!-- Jersey 配置 -->
  <servlet>
    <servlet-name>JerseyServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
        <!-- 系统启动时扫描的包的路径-->
      <param-value>com.goodsoft</param-value>
    </init-param>   
  </servlet>
  
  <servlet-mapping>
    <servlet-name>JerseyServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>



测试类
package com.goodsoft;

import java.io.InputStream;
import java.util.ArrayList;
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.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/test")
public class TestService {

	/**
	 * http://localhost:8080/webservice/test
	 */
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String helloWorld() {
		String ret = "Hello World!";
		return ret;
	}

	/**
	 * http://localhost:8080/webservice/test/userlist
	 */
	@Path("/userlist")
	@GET
	@Produces( { MediaType.APPLICATION_XML })
	public List<User> getUsers() {
		List<User> users = new ArrayList<User>();
		for (long i = 1; i < 5; i++) {
			User user = new User();
			user.setUserId(i);
			user.setUserName("yuhan" + i);
			user.setNickName("supben" + i);
			users.add(user);
		}
		return users;
	}

	// 解析path中的参数id 用@PathParam
	/**
	 * http://localhost:8080/webservice/test/userinfo/1
	 */
	@Path("/userinfo/{id}")
	@GET
	@Produces( { MediaType.APPLICATION_XML })
	public User getUserById(@PathParam("id") Long id) {
		User user = new User();
		user.setUserId(id);
		user.setUserName("yuhan");
		user.setNickName("supben");
		return user;
	}

	/**
	 * http://localhost:8080/webservice/test/upload
	 * 有几个文件,就有几对参数,InputStream注解封装了文件的二进制流
	 * ,FormDataContentDisposition注解封装了对于文件的解析结果(包括文件名,文件后缀等) 演示一次获取多个文件的方法
	 */
	@POST
	@Path("/upload")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	public Response upload(@FormDataParam("file1") InputStream in1, @FormDataParam("file1") FormDataContentDisposition fd1,
			@FormDataParam("file2") InputStream in2, @FormDataParam("file2") FormDataContentDisposition fd2) {
		System.out.println(fd1.getFileName() + "------");
		FileUtils.writeToFile(in1, "d:/1.jpg");
		FileUtils.writeToFile(in2, "d:/2.jpg");
		return Response.ok().build();
	}

	/**
	 * http://localhost:8080/webservice/test/params/3/4?id=1&name=shen&age=3
	 * 演示一次获取多个path参数(跟在/后面的)和query参数(跟在?后面的) 的方法
	 */
	@GET
	@Path("/params/{a}/{b}")
	@Produces(MediaType.TEXT_PLAIN)
	public String params(@Context UriInfo ui) {
		MultivaluedMap<String, String> pathvalueMap = ui.getPathParameters();
		MultivaluedMap<String, String> queryvalueMap = ui.getQueryParameters();
		for (String s : pathvalueMap.keySet()) {
			System.out.println("path参数名称为" + s + ",参数的值为" + pathvalueMap.get(s));
		}

		for (String s : queryvalueMap.keySet()) {
			System.out.println("query参数名称为" + s + ",参数的值为" + queryvalueMap.get(s));
		}
		return "params method invoke success!";
	}

	/**
	 * http://localhost:8080/webservice/test/form演示获取form的全部参数的方法
	 */
	@POST
	@Path("/form")
	@Produces(MediaType.TEXT_PLAIN)
	public String form(MultivaluedMap<String, String> formParams) {
		for (String s : formParams.keySet()) {
			System.out.println("form参数名称为" + s + ",参数的值为" + formParams.get(s));
		}
		return "form method invoke success!";
	}

	/**
	 * http://localhost:8080/webservice/test/formupload演示获取form的全部参数和上传的文件的方法
	 */
	@POST
	@Path("/formupload")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.TEXT_PLAIN)
	public String formupload(@FormDataParam("file") InputStream in, @FormDataParam("file") FormDataContentDisposition fd,
			@FormDataParam("name") String name) {
		System.out.println(name);

		System.out.println(fd.getFileName() + "------");
		FileUtils.writeToFile(in, "d:/3.jpg");
		return "formupload method invoke success!";
	}
}