在使用 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();
}

问题的类如下:

@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()方法不会带来同样的问题。

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

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

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

以下是我遇到的更多错误消息,希望可以帮助一些谷歌用户!

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]

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

相关内容

最新更新