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

No injection source found for a parameter in rest-service with jaxrs

我有问题,我的休息服务的两个方法在部署时出错,没有注入源。

我的服务是这样的:

@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();
}

Class 问题如下:

@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=[]}']]]

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

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

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

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

以下是我 运行 发现的更多错误消息,希望能对一些 google 员工有所帮助!

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.access0(ApplicationHandler.java:182) ~[jersey-server-2.21.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.call(ApplicationHandler.java:348) ~[jersey-server-2.21.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.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]

我发现,我正在使用 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