当前位置: 首页 > 知识库问答 >
问题:

如何为Swagger Codegen处理org.springframework.core.io.resource

贺光华
2023-03-14

我正在使用Spring Boot2开发一个web应用程序,其中有两个Spring项目协同工作:一个是通过REST API为应用程序逻辑提供服务的REST API服务器,另一个是负责呈现web页面和调用API服务器的web项目。web项目使用Swagger Codegen自动生成调用API的类。

@GetMapping("/files/{uuid}")
@ResponseBody
public org.springframework.core.io.Resource getFile(@PathVariable String uuid) {
    String systemPath = fileService.getFilePath(uuid);
    return new FileSystemResource(systemPath);
}
public io.swagger.client.model.Resource getFileUsingGET(String uuid) {...}
@GetMapping("/client/files/{uuid}")
@ResponseBody
public io.swagger.client.model.Resource getFile(@PathVariable String uuid) {
    return resourceControllerApi.getFileUsingGET(uuid);
}
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class io.swagger.client.model.Resource] and content type [image/jpeg]
/*
 * NPA Marketplace REST API
 * API to manage NPA Marketplace.
 *
 * OpenAPI spec version: 1.0
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */


package io.swagger.client.model;

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.client.model.InputStream;
import io.swagger.client.model.URI;
import io.swagger.client.model.URL;

/**
 * Resource
 */
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-03-08T14:55:08.754+07:00")
public class Resource {
  @JsonProperty("description")
  private String description = null;

  @JsonProperty("file")
  private java.io.File file = null;

  @JsonProperty("filename")
  private String filename = null;

  @JsonProperty("inputStream")
  private InputStream inputStream = null;

  @JsonProperty("open")
  private Boolean open = null;

  @JsonProperty("readable")
  private Boolean readable = null;

  @JsonProperty("uri")
  private URI uri = null;

  @JsonProperty("url")
  private URL url = null;

  public Resource description(String description) {
    this.description = description;
    return this;
  }

   /**
   * Get description
   * @return description
  **/
  @ApiModelProperty(value = "")
  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Resource file(java.io.File file) {
    this.file = file;
    return this;
  }

   /**
   * Get file
   * @return file
  **/
  @ApiModelProperty(value = "")
  public java.io.File getFile() {
    return file;
  }

  public void setFile(java.io.File file) {
    this.file = file;
  }

  public Resource filename(String filename) {
    this.filename = filename;
    return this;
  }

   /**
   * Get filename
   * @return filename
  **/
  @ApiModelProperty(value = "")
  public String getFilename() {
    return filename;
  }

  public void setFilename(String filename) {
    this.filename = filename;
  }

  public Resource inputStream(InputStream inputStream) {
    this.inputStream = inputStream;
    return this;
  }

   /**
   * Get inputStream
   * @return inputStream
  **/
  @ApiModelProperty(value = "")
  public InputStream getInputStream() {
    return inputStream;
  }

  public void setInputStream(InputStream inputStream) {
    this.inputStream = inputStream;
  }

  public Resource open(Boolean open) {
    this.open = open;
    return this;
  }

   /**
   * Get open
   * @return open
  **/
  @ApiModelProperty(value = "")
  public Boolean isOpen() {
    return open;
  }

  public void setOpen(Boolean open) {
    this.open = open;
  }

  public Resource readable(Boolean readable) {
    this.readable = readable;
    return this;
  }

   /**
   * Get readable
   * @return readable
  **/
  @ApiModelProperty(value = "")
  public Boolean isReadable() {
    return readable;
  }

  public void setReadable(Boolean readable) {
    this.readable = readable;
  }

  public Resource uri(URI uri) {
    this.uri = uri;
    return this;
  }

   /**
   * Get uri
   * @return uri
  **/
  @ApiModelProperty(value = "")
  public URI getUri() {
    return uri;
  }

  public void setUri(URI uri) {
    this.uri = uri;
  }

  public Resource url(URL url) {
    this.url = url;
    return this;
  }

   /**
   * Get url
   * @return url
  **/
  @ApiModelProperty(value = "")
  public URL getUrl() {
    return url;
  }

  public void setUrl(URL url) {
    this.url = url;
  }


  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Resource resource = (Resource) o;
    return Objects.equals(this.description, resource.description) &&
        Objects.equals(this.file, resource.file) &&
        Objects.equals(this.filename, resource.filename) &&
        Objects.equals(this.inputStream, resource.inputStream) &&
        Objects.equals(this.open, resource.open) &&
        Objects.equals(this.readable, resource.readable) &&
        Objects.equals(this.uri, resource.uri) &&
        Objects.equals(this.url, resource.url);
  }

  @Override
  public int hashCode() {
    return Objects.hash(description, file, filename, inputStream, open, readable, uri, url);
  }


  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Resource {\n");

    sb.append("    description: ").append(toIndentedString(description)).append("\n");
    sb.append("    file: ").append(toIndentedString(file)).append("\n");
    sb.append("    filename: ").append(toIndentedString(filename)).append("\n");
    sb.append("    inputStream: ").append(toIndentedString(inputStream)).append("\n");
    sb.append("    open: ").append(toIndentedString(open)).append("\n");
    sb.append("    readable: ").append(toIndentedString(readable)).append("\n");
    sb.append("    uri: ").append(toIndentedString(uri)).append("\n");
    sb.append("    url: ").append(toIndentedString(url)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }

}

共有1个答案

阎唯
2023-03-14

错误

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class io.swagger.client.model.Resource] and content type [image/jpeg] 

是从线路上发射的

ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);

在类io.Swagger.client.apiclient中,它是由variant库的Swagger Codegenresttemplate自动生成的,即相当于运行命令java-jar swagger-codegen-cli.jar generate-i api-specs.json-l java--library resttemplate

@GetMapping("/files/{uuid}")
@ResponseBody
public ResponseEntity<byte[]> getFile(@PathVariable String uuid) {
    FileMetadata myFileData = fileService.getFileMetadata(uuid);
    org.springframework.core.io.Resource res = new FileSystemResource(myFileData.getPaht());
    byte[] data = IOUtils.toByteArray(res.getInputStream());
    HttpHeaders respHeaders = new HttpHeaders();
    respHeaders.setContentType(MediaType.valueOf(myFileData.getContentType()));
    respHeaders.setContentLength(res.getFile().length());
    respHeaders.setContentDispositionFormData("attachment", myFileData.getName());
    return new ResponseEntity<>(data ,respHeaders, HttpStatus.OK);
}

web客户端服务器上的控制器

@GetMapping("/client/files/{uuid}")
@ResponseBody
public ResponseEntity<org.springframework.core.io.Resource> getFile(@PathVariable String uuid) {
    byte[] data = resourceControllerApi.getFileUsingGET(uuid);
    HttpHeaders responseHeader = (HttpHeaders) RequestContextHolder.getRequestAttributes().getAttribute("responseHeader", RequestAttributes.SCOPE_REQUEST);
    return new ResponseEntity<>(new ByteArrayResource(data), responseHeader, HttpStatus.OK);
}

关于我用来检索响应头的方式,由于我无法从生成的api类中获取响应头,所以我必须创建ClientHTTPrequestInterceptor来获取头并将其作为请求属性。

public class ResponseHeaderClientRequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        ClientHttpResponse response = execution.execute(request, body);
        HttpHeaders responseHeader = response.getHeaders();
        RequestAttributes requestAttrs = RequestContextHolder.getRequestAttributes();
        if (requestAttrs != null) {
            requestAttrs.setAttribute("responseHeader", responseHeader, RequestAttributes.SCOPE_REQUEST);
        }
        return response;
    }
}
 类似资料:
  • 问题内容: Java中有没有办法处理收到的SIGTERM? 问题答案: 是的,您可以向注册一个关闭挂钩。

  • 问题内容: 我收到此错误消息: 当我将其复制到浏览器地址字段中时,它显示了正确的页面,这是有效的,但是我不能用以下方法对其进行解析: 我试过了:,但是 这不是我需要的网址 也不行 如何处理呢? 坦率 问题答案: 对字符使用编码,即。

  • 问题内容: 我是遵循Java教程的Java初学者。 我正在使用Java教程的 “ 数据流”页面中的简单Java程序,并在运行时一直显示。我想知道这是否正常,因为读者最终必须走到文件末尾。 编译正常,但输出为: 在Java教程的 “ 数据流页面”中,它显示: 注意,DataStreams通过捕获EOFException而不是测试无效的返回值来检测文件结束条件。DataInput方法的所有实现都使用E

  • 我正在为一个鼠标悬停工作,我想通过使用for loop单击每个链接来测试所有链接的工作状态。在我的程序中,迭代进行了一次,而下一次迭代却不工作了,并且显示“StaleElementReferenceException”..........如果需要,请修改代码....

  • 神话这个词有时候意味着虚构。但这有着更深层的内涵。它也意味着一些宗教内容来解释宇宙和人类与之的关系。管理者倾向于忘记他们作为一个程序员时学到的东西,并且相信某种传说。试着让他们相信这种传说是错的,正如让一个虔诚的宗教信徒从他们的信仰中醒悟过来一样粗鲁而失败。因此,你应该认可这些信仰: 文档越多越好。(他们需要文档,但他们不会想要你在这些东西上花时间。) 程序员是平等的。(程序员可以按重要程度分类。