Spring GraphQL 不填充记录类

问题描述 投票:0回答:1

我正在使用 Spring Boot GraphQL 构建 midia 跟踪器,但是当我尝试发送突变来创建用户时,会抛出流动错误:

2024-05-26T21:20:12.078-03:00 ERROR 20896 --- [geek] [nio-8080-exec-1] s.g.e.ExceptionResolversExceptionHandler : Unresolved NullPointerException for executionId 03905c4e-a9b2-6c2d-49f7-f348b75165ee

java.lang.NullPointerException: Cannot invoke "com.espacogeek.geek.inputs.UserInput.password()" because "userInput" is null
        at com.espacogeek.geek.controllers.UserController.registerUser(UserController.java:43) ~[classes/:na]
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
        at org.springframework.graphql.data.method.InvocableHandlerMethodSupport.doInvoke(InvocableHandlerMethodSupport.java:87) ~[spring-graphql-1.2.6.jar:1.2.6]
        at org.springframework.graphql.data.method.annotation.support.DataFetcherHandlerMethod.validateAndInvoke(DataFetcherHandlerMethod.java:196) ~[spring-graphql-1.2.6.jar:1.2.6]
        at org.springframework.graphql.data.method.annotation.support.DataFetcherHandlerMethod.invoke(DataFetcherHandlerMethod.java:126) ~[spring-graphql-1.2.6.jar:1.2.6]
        at org.springframework.graphql.data.method.annotation.support.DataFetcherHandlerMethod.invoke(DataFetcherHandlerMethod.java:107) ~[spring-graphql-1.2.6.jar:1.2.6]
        at org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer$SchemaMappingDataFetcher.get(AnnotatedControllerConfigurer.java:680) ~[spring-graphql-1.2.6.jar:1.2.6]
        at org.springframework.graphql.execution.ContextDataFetcherDecorator.lambda$get$0(ContextDataFetcherDecorator.java:87) ~[spring-graphql-1.2.6.jar:1.2.6]
        at io.micrometer.context.ContextSnapshot.lambda$wrap$1(ContextSnapshot.java:106) ~[context-propagation-1.0.6.jar:1.0.6]
        at org.springframework.graphql.execution.ContextDataFetcherDecorator.get(ContextDataFetcherDecorator.java:87) ~[spring-graphql-1.2.6.jar:1.2.6]
        at org.springframework.graphql.observation.GraphQlObservationInstrumentation.lambda$instrumentDataFetcher$3(GraphQlObservationInstrumentation.java:145) ~[spring-graphql-1.2.6.jar:1.2.6]
        at graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation.lambda$instrumentDataFetcher$0(DataLoaderDispatcherInstrumentation.java:90) ~[graphql-java-21.4.jar:na]
        at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:329) ~[graphql-java-21.4.jar:na]
        at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:305) ~[graphql-java-21.4.jar:na]
        at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:243) ~[graphql-java-21.4.jar:na]
        at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:214) ~[graphql-java-21.4.jar:na]
        at graphql.execution.ExecutionStrategy.resolveField(ExecutionStrategy.java:186) ~[graphql-java-21.4.jar:na]
        at graphql.execution.AsyncSerialExecutionStrategy.lambda$execute$1(AsyncSerialExecutionStrategy.java:56) ~[graphql-java-21.4.jar:na]  
        at graphql.execution.Async.eachSequentiallyImpl(Async.java:162) ~[graphql-java-21.4.jar:na]
        at graphql.execution.Async.eachSequentially(Async.java:151) ~[graphql-java-21.4.jar:na]
        at graphql.execution.AsyncSerialExecutionStrategy.execute(AsyncSerialExecutionStrategy.java:51) ~[graphql-java-21.4.jar:na]
        at graphql.execution.Execution.executeOperation(Execution.java:162) ~[graphql-java-21.4.jar:na]
        at graphql.execution.Execution.execute(Execution.java:104) ~[graphql-java-21.4.jar:na]
        at graphql.GraphQL.execute(GraphQL.java:568) ~[graphql-java-21.4.jar:na]
        at graphql.GraphQL.lambda$parseValidateAndExecute$13(GraphQL.java:487) ~[graphql-java-21.4.jar:na]
        at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1187) ~[na:na]
        at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2341) ~[na:na]
        at graphql.GraphQL.parseValidateAndExecute(GraphQL.java:482) ~[graphql-java-21.4.jar:na]
        at graphql.GraphQL.lambda$executeAsync$9(GraphQL.java:440) ~[graphql-java-21.4.jar:na]
        at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1187) ~[na:na]
        at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2341) ~[na:na]
        at graphql.GraphQL.executeAsync(GraphQL.java:428) ~[graphql-java-21.4.jar:na]
        at org.springframework.graphql.execution.DefaultExecutionGraphQlService.lambda$execute$2(DefaultExecutionGraphQlService.java:99) ~[spring-graphql-1.2.6.jar:1.2.6]
        at reactor.core.publisher.MonoDeferContextual.subscribe(MonoDeferContextual.java:47) ~[reactor-core-3.6.5.jar:3.6.5]
        at reactor.core.publisher.Mono.subscribe(Mono.java:4568) ~[reactor-core-3.6.5.jar:3.6.5]
        at reactor.core.publisher.Mono.subscribeWith(Mono.java:4634) ~[reactor-core-3.6.5.jar:3.6.5]
        at reactor.core.publisher.Mono.toFuture(Mono.java:5146) ~[reactor-core-3.6.5.jar:3.6.5]
        at org.springframework.graphql.server.webmvc.GraphQlHttpHandler.handleRequest(GraphQlHttpHandler.java:113) ~[spring-graphql-1.2.6.jar:1.2.6]
        at org.springframework.web.servlet.function.support.HandlerFunctionAdapter.handle(HandlerFunctionAdapter.java:108) ~[spring-webmvc-6.1.6.jar:6.1.6]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089) ~[spring-webmvc-6.1.6.jar:6.1.6]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) ~[spring-webmvc-6.1.6.jar:6.1.6]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) ~[spring-webmvc-6.1.6.jar:6.1.6]       
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.1.6.jar:6.1.6]
        at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590) ~[tomcat-embed-core-10.1.20.jar:6.0]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.1.6.jar:6.1.6]
        at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) ~[tomcat-embed-core-10.1.20.jar:6.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) ~[tomcat-embed-websocket-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.1.6.jar:6.1.6]  
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.1.6.jar:6.1.6]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.1.6.jar:6.1.6]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.1.6.jar:6.1.6]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.springframework.web.filter.ServerHttpObservationFilter.doFilterInternal(ServerHttpObservationFilter.java:109) ~[spring-web-6.1.6.jar:6.1.6]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.1.6.jar:6.1.6]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.1.6.jar:6.1.6]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.1.6.jar:6.1.6]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) ~[tomcat-embed-core-10.1.20.jar:10.1.20]       
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) ~[tomcat-embed-core-10.1.20.jar:10.1.20]        
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) ~[tomcat-embed-core-10.1.20.jar:10.1.20]    
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.20.jar:10.1.20]   
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736) ~[tomcat-embed-core-10.1.20.jar:10.1.20]       
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.20.jar:10.1.20]
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.20.jar:10.1.20] 
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63) ~[tomcat-embed-core-10.1.20.jar:10.1.20]        
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]

因为当调用 registerUser() 时调试 UserInput 为 null 时,我对 GraphQL 做的错误不会填充 UserInput?有人能帮助我吗? 如果您需要更多信息,请告诉我。

要求:

mutation User {
  registerUser(
    user: {
      username: "Biga",
      email: "[email protected]",
      password: "test"
    }
  ) {
    username
  }
}

用户.graphqls:

type Query {
    findUserByUsername(username: String!): User
    findUserById(id: ID!): User
    findUserByEmail(email: String!): User
}

type Mutation {
    registerUser(user: UserInput!): User
}

type User {
    id: ID
    username: String
    email: String
}

input UserInput {
    username: String!
    email: String!
    password: String!
}

用户输入.java

package com.espacogeek.geek.inputs;

public record UserInput(String username, String email, String password) {}

用户控制器.java

package com.espacogeek.geek.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.stereotype.Controller;

import com.espacogeek.geek.inputs.UserInput;
import com.espacogeek.geek.modals.UserModal;
import com.espacogeek.geek.services.User.UserService;

import at.favre.lib.crypto.bcrypt.BCrypt;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //Some codes...

    @MutationMapping
    public UserModal registerUser(@Argument UserInput userInput) {
        var passwordCrypted = BCrypt.withDefaults().hash(12, userInput.password().toCharArray());
        UserModal user = new UserModal(null, userInput.username(), userInput.email(), passwordCrypted);
        return userService.save(user);
    }
}
java spring spring-boot spring-mvc graphql
1个回答
0
投票

我通过将

name
参数添加到
@Argument
注释来解决此错误。我还将记录更改为类并添加了 get Getter 和 Setter。

要求:

mutation User {
    registerUser(newUser: {username: "BigaArtz", email: "[email protected]", password: "biga123"}){
        username
    }
}

graphql:

type Mutation {
    registerUser(newUser: NewUser): User
}

input NewUser {
    username: String!
    email: String!
    password: String!
}

NewUser.java

@Getter
@Setter
public class NewUser implements Serializable {
    private String username;
    private String email;
    private String password;
}

用户控制器.java

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @QueryMapping
    public Optional<UserModal> findUser(@Argument(name = "filter") UserInput userInput) {
        return userService.findByUsernameContains(userInput.getUsername());
    }

    @MutationMapping
    public UserModal registerUser(@Argument(name = "newUser") NewUser newUser) {
        var passwordCrypted = BCrypt.withDefaults().hash(12, newUser.getPassword().toCharArray());
        UserModal user = new UserModal(null, newUser.getUsername(), newUser.getEmail(), passwordCrypted);
        return userService.save(user);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.