【问题标题】:Unable to find contextual data of type: io.vertx.core.http.HttpServerRequesta找不到类型的上下文数据:io.vertx.core.http.HttpServerRequesta
【发布时间】:2020-05-07 15:38:51
【问题描述】:

我在运行 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

我的端点类是

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

}

第二个 Endpoint 类:

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

}

这是一个控制台日志返回: RESTEASY003880:找不到类型的上下文数据:io.vertx.core.http.HttpServerRequest 在 org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:120) 在 com.sun.proxy.$Proxy70.remoteAddress(未知来源) 在 com.ciwara.kalanSowApp.web.filter.LoggingFilter.filter(LoggingFilter.java:27) 在 org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:311) 在 org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:423) 在 org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:391) 在 org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invoke$1(ResourceMethodInvoker.java:365) 在 java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995) 在 java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137) 在 java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:110) 在 org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365) 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:477) 在 org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:252) 在 org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153) 在 org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363) 在 org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156) 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238) 在 org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:249) 在 io.quarkus.resteasy.runtime.ResteasyFilter.doFilter(ResteasyFilter.java:30) 在 io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) 在 io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) 在 com.ciwara.kalanSowApp.security.cors.CORSFilter.doFilter(CORSFilter.java:62) 在 io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) 在 io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) 在 io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84) 在 io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:63) 在 io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68) 在 io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) 在 io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:133) 在 io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) 在 io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

非常感谢您帮助我。

【问题讨论】:

    标签: java rest quarkus


    【解决方案1】:

    您的资源类中缺少与您的端点匹配的 @Path。假设您要调用第一个类资源的方法 allUsers,然后在类级别添加 @Path("api") 并在方法级别添加 @Path("users") :

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

    你的方法

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

    而且我更喜欢将 -X GET 添加到您的 curl 命令中(您可以稍后更改为不同的 http 方法)。

    【讨论】:

    • 您可以将问题标记为已回答,以便其他人知道它有正确答案。
    猜你喜欢
    • 2014-07-03
    • 2015-12-02
    • 2023-02-16
    • 1970-01-01
    • 2022-01-08
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多