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 {
/**
* 初始化三個用戶數(shù)據(jù),存入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/*
摘要:如果是其它方式引發(fā)的錯誤,此處的解決方法不可用。最與該錯誤重要便是前兩個。仔細檢查發(fā)現(xiàn),在接口中將參數(shù)轉化為一個對象,但沒有指出該對象就是錯誤代碼根據(jù)查詢條件查詢登錄注冊訪問統(tǒng)計數(shù)據(jù)解決辦法接口中添加相應的注解。
1. object is not an instance of declaring class
錯誤日日志
2016-06-01 10:55:59,612 [RMI TCP...