Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
修复项目中 spring 封装下的接口所存在的问题 (#60)
Browse files Browse the repository at this point in the history
* fix-bug

* fix-bug
  • Loading branch information
A-Little-Excited authored May 15, 2024
1 parent 15bb0e4 commit 8479338
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 Paion Data
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.paiondata.athena.spring.boot.autoconfigure.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* Configuration for the web
* <p>
* This configuration is used to add a prefix to all the endpoints.
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {

/**
* Add a prefix to all the endpoints.
*
* @param configurer the path match configurer
*/
@Override
public void configurePathMatch(final PathMatchConfigurer configurer) {
configurer.addPathPrefix("/v1", c -> true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -34,11 +36,9 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
Expand Down Expand Up @@ -109,27 +109,26 @@ public Map<String, String> uploadFile(
* Receive a request to download a file and find the corresponding file on Ali OSS based on the fileId.
*
* @param fileId The fileId of the file requested to be downloaded
* @param response The built-in HttpServletResponse object that represents the response
*
* @return the inputStream of the file content
* @return the file content
*
* @throws NullPointerException if {@code fileId} is {@code null}
*/
@GetMapping(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public InputStream downloadFile(@NotNull final String fileId, @NotNull final HttpServletResponse response) {
public ResponseEntity<InputStreamResource> downloadFile(@NotNull @RequestParam(FILE_ID) final String fileId) {
Objects.requireNonNull(fileId);
Objects.requireNonNull(response);

response.setHeader(
"content-disposition",
String.format(
"attachment; filename = %s",
((Map<?, ?>) ((Map<?, ?>) metaStore
.getMetaData(fileId, Collections.singletonList(MetaData.FILE_NAME))
.toSpecification().get("data")).get("metaData"))
.get(MetaData.FILE_NAME).toString()
));

return fileStore.download(Objects.requireNonNull(fileId));
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(
"content-disposition",
String.format(
"attachment; filename = %s",
((Map<?, ?>) ((Map<?, ?>) metaStore
.getMetaData(fileId, Collections.singletonList(MetaData.FILE_NAME))
.toSpecification().get("data")).get("metaData"))
.get(MetaData.FILE_NAME).toString()
))
.body(new InputStreamResource(fileStore.download(fileId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import graphql.ExecutionResult;
Expand Down Expand Up @@ -64,7 +66,7 @@ public class MetaController {
* @throws NullPointerException if {@code query} is {@code null}
*/
@GetMapping
public ExecutionResult get(@NotNull final String query) {
public ExecutionResult get(@NotNull @RequestParam("query") final String query) {
return metaStore.executeNative(Objects.requireNonNull(query));
}

Expand All @@ -86,7 +88,7 @@ public ExecutionResult get(@NotNull final String query) {
* @throws IllegalArgumentException if no metadata fields are found in {@code graphQLDocument}
*/
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public ExecutionResult post(@NotNull final String graphQLDocument) {
public ExecutionResult post(@NotNull @RequestBody final String graphQLDocument) {
Objects.requireNonNull(graphQLDocument);

final List<String> requestedMetadataFields = jsonDocumentParser.getFields(graphQLDocument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FileControllerITSpec extends AbstractITSpec {

MockHttpServletRequestBuilder builder =
MockMvcRequestBuilders
.multipart("/file/upload")
.multipart("/v1/file/upload")
.file(mockMultipartFile)

then:
Expand Down

0 comments on commit 8479338

Please sign in to comment.