【发布时间】:2015-07-13 18:44:35
【问题描述】:
我正在尝试通过关注this tutorial 在我当前的 Java Restful API (Jersey) 上实现身份验证。我将所有新的身份验证类放在“auth”包中。当我运行 jquery 示例代码时,它说“DemoBusinessRESTResourceProxy 是一个接口,不能被实例化”。所以我研究并决定将Jersey注解放在DemoBusinessRESTResource上并删除DemoBusinessRESTResourceProxy:
@Local
@Path("access/")
@Stateless(name = "DemoBusinessRESTResource", mappedName = "ejb/DemoBusinessRESTResource")
public class DemoBusinessRESTResource {
private static final long serialVersionUID = -6663599014192066936L;
@POST
@Path("/login")
@Produces(MediaType.APPLICATION_JSON)
public Response login(@Context HttpHeaders httpHeaders,
@FormParam("username") String username,
@FormParam("password") String password) {
Authenticator authenticator = Authenticator.getInstance();
String serviceKey = httpHeaders
.getHeaderString(HTTPHeaderNames.SERVICE_KEY);
try {
String authToken = authenticator.login(serviceKey, username,
password);
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("auth_token", authToken);
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.OK).entity(
jsonObj.toString()).build();
} catch (final LoginException ex) {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message",
"Problem matching service key, username and password");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.UNAUTHORIZED)
.entity(jsonObj.toString()).build();
}
}
@GET
@Path("/demo-get-method")
@Produces(MediaType.APPLICATION_JSON)
public Response demoGetMethod() {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message", "Executed demoGetMethod");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.OK).entity(
jsonObj.toString()).build();
}
@POST
@Path("/demo-post-method")
@Produces(MediaType.APPLICATION_JSON)
public Response demoPostMethod() {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message", "Executed demoPostMethod");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.ACCEPTED).entity(
jsonObj.toString()).build();
}
@POST
@Path("/logout")
public Response logout(@Context HttpHeaders httpHeaders) {
try {
Authenticator authenticator = Authenticator.getInstance();
String serviceKey = httpHeaders
.getHeaderString(HTTPHeaderNames.SERVICE_KEY);
String authToken = httpHeaders
.getHeaderString(HTTPHeaderNames.AUTH_TOKEN);
authenticator.logout(serviceKey, authToken);
return getNoCacheResponseBuilder(Response.Status.NO_CONTENT)
.build();
} catch (final GeneralSecurityException ex) {
return getNoCacheResponseBuilder(
Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
private Response.ResponseBuilder getNoCacheResponseBuilder(
Response.Status status) {
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMaxAge(-1);
cc.setMustRevalidate(true);
return Response.status(status).cacheControl(cc);
}
}
现在,我收到了这个错误:
在调用 EJB BusinessRESTResource 期间发生系统异常,方法:public javax.ws.rs.core.Response com.rest.auth.DemoBusinessRESTResource.login(javax.ws.rs.core.HttpHeaders,java.lang. String,java.lang.String),引起:java.lang.AbstractMethodError: com.sun.jersey.spi.container.ContainerRequest.getHeaderString(Ljava/lang/String;)Ljava/lang/String;
我是 Java WS 的新手,我完全迷路了。
【问题讨论】: