使用jaxrs在rest-service中找不到参数的注入源

时间:2015-01-14 10:03:43

标签: rest java-ee glassfish jax-rs

我遇到了问题,我的休息服务的两种方法会在部署时带来错误,没有注入源。

我的服务如下:

@Path("/chatservice")
public class ChatServiceImpl implements ChatService{

@POST
@Path("/registerToServer")
@Consumes(MediaType.APPLICATION_JSON)
@Override
public Response registerToServer(User user) {

    UserList userListObject = getAllChatableUsers(user);

    return Response.status(200).entity(userListObject).build();
}

@POST
@Path("/sendMessage")
@Consumes(MediaType.APPLICATION_JSON)
@Override
public Response sendMessage(Message message) {
    boolean isSuccess = putMessageIntoDatabase(message);

    return Response.status(200).build();
}

@POST
@Path("/getAllMessagesForUser")
@Consumes(MediaType.APPLICATION_JSON)
@Override
public Response getAllMessagesForUser(UserWithRecipient userWithRecipient) {

    return Response.status(200).build();
}

@POST
@Path("/getAllMessagesForUser/{numberOfMessages}")
@Consumes(MediaType.APPLICATION_JSON)
@Override
public Response getMessagesForUser(@PathParam("numberOfMessages") int numberOfMessages, UserWithRecipient userWithRecipient) {

    return Response.status(200).build();
}

问题的类如下:

@XmlSeeAlso(User.class)
@XmlRootElement
public class UserWithRecipient {

private User user;
private User recipient;

public UserWithRecipient() {
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public User getRecipient() {
    return recipient;
}

public void setRecipient(User recipient) {
    this.recipient = recipient;
}
}

我得到的错误如下:

 [[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response de.hszg.fei.ws.service.ChatServiceImpl.getMessagesForUser(int,de.hszg.fei.ws.model.UserWithRecipient) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[application/json], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class de.hszg.fei.ws.service.ChatServiceImpl, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@6da34189]}, definitionMethod=public javax.ws.rs.core.Response de.hszg.fei.ws.service.ChatServiceImpl.getMessagesForUser(int,de.hszg.fei.ws.model.UserWithRecipient), parameters=[Parameter [type=int, source=numberOfMessages, defaultValue=null], Parameter [type=class de.hszg.fei.ws.model.UserWithRecipient, source=null, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']]]

你能告诉我这堂课有什么问题。我也不明白为什么sendMessage()方法不会带来同样的问题。

3 个答案:

答案 0 :(得分:22)

我发现问题是,我输入了错误的@PathParam注释。

答案 1 :(得分:2)

添加此答案,为问题/接受的答案添加更多上下文。

在他的回答中描述的Daniel Müssig,我也使用了错误的@PathParam注释(我使用的是javax.websocket.server中的注释,但显然它应该是来自javax.ws.rs的注释)。

以下是我遇到的一些错误消息,可能有助于一些googlers在那里!

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type [... more specific method data on this line]
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:553) ~[jersey-server-2.21.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:182) ~[jersey-server-2.21.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:348) ~[jersey-server-2.21.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:345) ~[jersey-server-2.21.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) ~[jersey-common-2.21.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) ~[jersey-common-2.21.jar:na]
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255) ~[jersey-common-2.21.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:345) ~[jersey-server-2.21.jar:na]
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:390) ~[jersey-container-servlet-core-2.21.jar:na]

答案 2 :(得分:0)

我发现,我正在使用 com.sun.jersey.multipart.FormDataParam 。正确导入是 org.glassfish.jersey.media.multipart.FormDataParam 。这解决了我的问题。

参考 - https://github.com/jersey/jersey/blob/master/examples/multipart-webapp/src/main/java/org/glassfish/jersey/examples/multipart/webapp/MultiPartFieldInjectedResource.java