Resteasy配置及其使用

      resteasy秉承restful风格,它的简单已配置比使用servlet更加方便。它支持PUT   、 POST 、  GET  等常用HTTP请求方式,并且提供annotation的 Java类可配置功能将Java对象数据JSON转化。

1、访问方式

  PUT    POST   GET   (常用HTTP请求方式)
2、下载资源jar包支持[如图]
Resteasy配置及其使用_第1张图片
3、web.xml配置


		resteasy.resources
		com.rest.UserService
	
	
	
		resteasy.servlet.mapping.prefix
		/service
	


	
		
			org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
		
	


	
		Resteasy
		org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
	


	
		Resteasy
		/services/*
	
4、编写控制访问
package com.rest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
//import javax.ws.rs.FormParam;//获取表单参数
import javax.ws.rs.Produces;
//import javax.ws.rs.core.Context;//请求上下文HttpServletRequest
//import javax.ws.rs.QueryParam;//用于获取数据库查询参数
@Path("userservice")
public class UserService {
	/**
	 * 初始化用户数据
	 */
	static Map map=new HashMap();
	
	static{
		User user1=new User("linda","623423","[email protected]",23,"female");
		map.put(1, user1);
		User user2=new User("veppa","734423","[email protected]",21,"male");
		map.put(2, user2);
		User user3=new User("nokka","973423","[email protected]",23,"female");
		map.put(3, user3);
	}
	
	/**
	 * 获取指定Id的用户信息
	 * @param id
	 * @return
	 */
	@GET
	@Path("user/{id}")
	@Produces("application/json")
	public User getById(@PathParam("id") Integer id) {
	    return (User) map.get(id);
	}
	
	/**
	 * 获取所有用户列表的JSON数据
	 * @return
	 */
	@GET
	@Path("users")
	@Produces("application/json")
	public List getUsers() {
		List users=new ArrayList();
		for(Integer index: map.keySet()){
			User user=(User) map.get(index);
			users.add(user);
		}
	    return users; 
	}
	
	/**
	 * 获取的用户传入的信息
	 * @param msg
	 * @return
	 */
	@GET
	@Path("user/trans/{msg}")
	public String  getMessage(@PathParam("msg") String msg) {
	    return "[Hello dear! ]"+",MSG:{"+msg+"}";
	}


}
5、测试
  输入访问:http://localhost:8080/resteasy/services/userservice/users
 打印结果: [{"email":"[email protected]","age":23,"gender":"female","name":"linda","password":"623423"},
 {"email":"[email protected]","age":21,"gender":"male","name":"veppa","password":"734423"},
 {"email":"[email protected]","age":23,"gender":"female","name":"nokka","password":"973423"}]

你可能感兴趣的:(APIService)