简体   繁体   中英

Unable to find contextual data of type: io.vertx.core.http.HttpServerRequesta

I found a exception when I run curl -d '{"DTYPE":"3","id":3, "version":1,"email":"ajj@example.com", "firstName":"Didier", "lastName":"Ben","password":"Fatoumata7899","username":fatou79" }' -H 'Content-Type:application/json' http://localhost:8080/api/users

My endpoint class is

    @ApplicationScoped
public class UsersResource {

    @Context
    private UriInfo uriInfo;


    @Inject
    private UserRepository users;



    /*@Crypto(Type.BCRYPT)*/
/*  @Inject
    @Context
    private PasswordEncoder passwordEncoder;*/

    @GET
    public User getUserById(@PathParam ("id")Long id) {


        return users.findById(id);
    }
    @GET
    public Response allUsers() {
        return ok(users.findAll()).build();
    }

    @GET
    @Path("/count")
    public Response count() {
        return ok(Count.builder().count(users.stream().count()).build()).build();
    }

    @GET
    @Path("/exists")
    public Response exists(@QueryParam("username") String username, @QueryParam("email") String email) {
        if (username != null && username.length() > 0) {
            return ok(Existed.builder().existed(users.findByUsername(username).isPresent()).build()).build();
        }

        if (email != null && email.length() > 0) {
            return ok(Existed.builder().existed(users.findByEmail(email).isPresent()).build()).build();
        }

        return Response.status(Response.Status.BAD_REQUEST).entity("username or email query params is required")
                .build();
    }



    @POST
    public Response create(User user, @PathParam("user") Long userId) {
        User u = users.findUserById(userId);
        users.createUser(u);
        return Response.status(201).build();

    }

    @PUT 
    @Path("/{id}")
    public Response update(@PathParam ("id")Long id, User user) {

        if(!id.equals(user.getId())) {

            throw new BadRequestException("id in path and in Body must be the same");
        }
        users.updateUser(user);

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

    @DELETE
    @Path("/{id}")
    public Response delete(@PathParam("id") Long userId) {
        users.deleteById(userId);
        return Response.status(204).build();
    }

}

the second Endpoint class:

   @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApplicationScoped
public class UserResource {

    @Context
    UriInfo uriInfo;

    @Inject
    UserRepository users;

    @Context
    SecurityContext securityContext;

    @PathParam("username")
    String username;

    @GET
    public Response getUserByUsername() {

        return users.findByUsername(username)
                .map(u -> Response.ok(u)
                        .link(uriInfo.getBaseUriBuilder().path("/users/{username}").build(username), "self").build())
                .orElse(Response.status(Response.Status.NOT_FOUND).build());
    }

    @PUT
    public Response updateUser(UserForm form) {

        if (securityContext.getUserPrincipal() != null
                && securityContext.getUserPrincipal().getName().equals(username)) {
            return users.findByUsername(username).map(u -> {
                u.setFirstName(form.getFirstName());
                u.setLastName(form.getLastName());
                u.setEmail(form.getEmail());

                users.save(u);
                return Response.noContent().build();
            }).orElse(Response.status(Response.Status.NOT_FOUND).build());
        }

        return Response.status(Response.Status.UNAUTHORIZED).build();
    }

    @DELETE
    public Response deleteUser() {

        return users.findByUsername(username).map(u -> {
            users.delete(u);
            return Response.noContent().build();
        }).orElse(Response.status(Response.Status.NOT_FOUND).build());
    }

}

This a console log return: RESTEASY003880: Unable to find contextual data of type: io.vertx.core.http.HttpServerRequest at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:120) at com.sun.proxy.$Proxy70.remoteAddress(Unknown Source) at com.ciwara.kalanSowApp.web.filter.LoggingFilter.filter(LoggingFilter.java:27) at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:311) at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:423) at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:391) at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invoke$1(ResourceMethodInvoker.java:365) at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995) at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137) at java.util.concurrent.CompletableFuture.thenCom pose(CompletableFuture.java:110) at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:477) at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:252) at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153) at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363) at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238) at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:249) at io.quarkus.resteasy.runtime.ResteasyFilter.doFilter(ResteasyFilter.java:30) at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) at io.undertow.s ervlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) at com.ciwara.kalanSowApp.security.cors.CORSFilter.doFilter(CORSFilter.java:62) at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:63) at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:133) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) at io.undertow.serve r.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

Thank you very much to help me.

You are missing @Path to your resources class that will match your endpoint. Lets suppose that you want to call method allUsers of your first class resource then add @Path("api") on class level and @Path("users") on your method level:

@Path("api")
@ApplicationScoped
public class UsersResource {

and on your method

@Path("users")
@GET
    public Response allUsers() {
        return ok(users.findAll()).build();
    }

And I prefer to add -X GET to your curl command (you can change later to different http method).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM