package com.fengyuan.restapi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.fengyuan.domain.User;
@Path("userservice") // 服务路径
public class UserService {
/**
* 初始化三个用户数据,存入map中,key为用户id,value为用户对象
*/
static Map userMap = new HashMap<>();
static {
User user1 = new User("Lee", 24, "138***");
userMap.put(1, user1);
User user2 = new User("Cathy", 25, "188***");
userMap.put(2, user2);
User user3 = new User("Aaron", 26, "186***");
userMap.put(3, user3);
}
/**
* 获取指定id的用户
*
* @param id
* @return
*/
@GET
@Path("user/{id}") // 具体服务的路径, id是入参
@Produces("application/json") // 返回的格式
public User getById(@PathParam("id") Integer id) {
return (User) userMap.get(id);
}
/**
* 以json格式返回所有用户
*
* @return
*/
@GET
@Path("users")
@Produces("application/json")
public List getUsers() {
List userList = new ArrayList();
for (Entry user : userMap.entrySet()) {
userList.add(user.getValue());
}
return userList;
}
}
MessageService.java:
package com.fengyuan.restapi;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/messageservice")
public class MessageService {
public MessageService(){}
@GET
@Path("/{param}")
public Response printMessage(@PathParam("param") String msg) {
String result = "Hello : " + msg;
return Response.status(200).entity(result).build();
}
}
User.java:
package com.fengyuan.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
public @Data @AllArgsConstructor class User {
private String name;
private int age;
private String tel;
}
3. 配置web.xml:
Restful Web Applicationresteasy.scantrueresteasy.servlet.mapping.prefix/restorg.jboss.resteasy.plugins.server.servlet.ResteasyBootstrapresteasy-servletorg.jboss.resteasy.plugins.server.servlet.HttpServletDispatcherresteasy-servlet/rest/*
摘要:如果是其它方式引发的错误,此处的解决方法不可用。最与该错误重要便是前两个。仔细检查发现,在接口中将参数转化为一个对象,但没有指出该对象就是错误代码根据查询条件查询登录注册访问统计数据解决办法接口中添加相应的注解。
1. object is not an instance of declaring class
错误日日志
2016-06-01 10:55:59,612 [RMI TCP...