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

I have a problem that the two methods of the recreation service will bring about a deployment error, that there is no source of injection.

My service looks like this:

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

The class with the task is as follows:

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

And the error I get is the following:

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

Can you tell me what the problem is with this class. I also don't understand why the sendMessage() method does not pose the same problem.

+7
source share
4 answers

I found out that the problem was that I imported the wrong @PathParam Annotation.

+22
source

Adding this answer to add a bit more context to the question / accepted answer.

Like Daniel Mussig described in his answer, I also used the incorrect @PathParam annotation (I used one from javax.websocket.server, but obviously it should be one from javax.ws.rs).

Here are some more error messages I came across that might help some 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
source

I found myself using com.sun.jersey.multipart.FormDataParam . Import Right: org.glassfish.jersey.media.multipart.FormDataParam . This fixed my problem.

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

0
source

Another possible reason for this very common mistake is that Jersey only searches for factories associated with the last annotation when several parameters are declared in the parameter. (See Error Report )

Until this is fixed, if you use any other annotations besides @FormDataParam , they should be the last.

It works:

 @NotEmpty @FormDataParam("myParam") String myParam 

Is not:

 @FormDataParam("myParam") @NotEmpty String myParam 
0
source

Source: https://habr.com/ru/post/980979/


All Articles