【发布时间】:2018-06-11 13:00:04
【问题描述】:
我正在尝试将 GraphQL 突变和图像上传为应用程序/表单数据。 GraphQL 部分正在工作,但我想“保存”上传的二进制文件并将路径添加到 GraphQL 数据。在 createGraphQLContext 我可以访问 HttpServletRequest 但(多)部分是空的。 我使用带有嵌入式 tomcat 8.5 的 graphql-spring-boot-starter 和提供的 GraphQL Java Tools
这是我对 /graphql 的 Relay Modern 调用
------WebKitFormBoundaryWBzwQyVX0TvBTIBD
Content-Disposition: form-data; name="query"
mutation CreateProjectMutation(
$input: ProjectInput!
) {
createProject(input: $input) {
id
name
}
}
------WebKitFormBoundaryWBzwQyVX0TvBTIBD
Content-Disposition: form-data; name="variables"
{"input":{"name":"sdasas"}}
------WebKitFormBoundaryWBzwQyVX0TvBTIBD
Content-Disposition: form-data; name="file"; filename="51zvT5zy44L._SL500_AC_SS350_.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryWBzwQyVX0TvBTIBD--
在我的@Component
public class MyGraphQLContextBuilder implements GraphQLContextBuilder 中,我可以访问HttpServletRequest,并希望使用req.getPart( "file" ) 访问该文件
但我在请求中的部分是空的 intellij debugger
我已将此添加到我的 application.yml
spring:
http:
multipart:
enabled: true
file-size-threshold: 10MB
location: /tmp
max-file-size: 10MB
max-request-size: 15MB
resolve-lazily: false
并尝试了不同的@configuration 来启用多部分配置,但部分仍然是空的。
@Configuration
public class MultipartConfig {
@Bean
public MultipartResolver multipartResolver() {
StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
return resolver;
}
}
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { MultipartConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/graphql" };
}
@Override
protected void customizeRegistration(Dynamic registration) {
//Parameters:-
// location - the directory location where files will be stored
// maxFileSize - the maximum size allowed for uploaded files
// maxRequestSize - the maximum size allowed for multipart/form-data requests
// fileSizeThreshold - the size threshold after which files will be written to disk
MultipartConfigElement multipartConfig = new MultipartConfigElement("/tmp", 1048576,
10485760, 0);
registration.setMultipartConfig(multipartConfig);
}
}
我不知道该怎么做。希望有人可以帮助我。
谢谢。
【问题讨论】:
标签: java spring spring-boot graphql