Denis Kisina
Denis Kisina

Reputation: 404

Pass a list of enums in postman request body

This is the controller that I am trying to pass the enum values to:

 @AllowedRequestParams({"columns", "ids"})
  @GetMapping("/v1/analysis/files")
  public ResponseEntity<byte[]> downloadAnalysis(@Valid AnalysisColumnsSelector columnsSelector,
                                                 @Valid FileUploadSearchCriteria criteria,
                                                 @Valid AnalysisSortable sortable) {
    return csv()
        .asAttachment("data_analysis.csv")
        .withBody(AnalysisDownloader.downloadAnalysis(columnsSelector, criteria, sortable));
  }

This is the object having a list of enums that I am trying to pass:


  import static java.util.Arrays.asList;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;

import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Setter;

  @EqualsAndHashCode
  @Setter
  class AnalysisColumnsSelector {

    private List<AnalysisColumnEnum> columns;

    public List<AnalysisColumnEnum> getColumns() {
      return isEmpty(columns) ? asList(AnalysisColumnEnum.values()) : columns;
    }

    public String[] getHeaders() {
      return getColumns().stream()
          .map(AnalysisColumnEnum::getHeader)
          .toArray(String[]::new);
    }
  }

and this is the enum:

 import static java.util.Arrays.stream;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

  @Getter
  @RequiredArgsConstructor
  enum AnalysisColumnEnum {

    ANALYSIS_COLUMN("analysisId", "[![ANALYSIS][1]][1]"),

    TITLE("title", "TITLE"),

    DISCUSSED("discussed", "Discussed");

    private final String code;
    private final String header;

    public static AnalysisColumnEnum decode(String code) {
      return stream(values())
          .filter(analysisColumn -> analysisColumn.code.equals(code))
          .findFirst()
          .orElseThrow(() -> new IllegalArgumentException("Unknown column code."));
    }

I am passing my request body as in the below image, but the values are not being sent to the controller. What is my mistake? enter image description here

Upvotes: 0

Views: 2216

Answers (1)

JeffreyXue
JeffreyXue

Reputation: 21

your request method is GET, but you send request body, they are not match. I suggest you change your request method turn to POST, and use @RequestBody. By the way, You need to encapsulate three objects into a new object.

@AllowedRequestParams({"columns", "ids"})
@PostMapping("/v1/analysis/files")
public ResponseEntity<byte[]> downloadAnalysis(@RequestBody XX xx) {
    return csv()
            .asAttachment("data_analysis.csv")
            .withBody(AnalysisDownloader.downloadAnalysis(xx.getColumnsSelector(), xx.getCriteria(), xx.getSortable()));
}

Upvotes: 2

Related Questions