How to change form data body feign client interceptor

1.4k Views Asked by At

I have some apis with content-type: form-data. Every request has some common fields in the body Here is my code:

package com.example.demo.request;


import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;


public class CreateNhanhProductRequest {
    private String nuctk;
    private Long storeId;
    private String name;
    private Integer typeId;
    private String parentIdName;
    private Long parentId;
    private String code;
    private String barcode;
    private BigDecimal importPrice;
    private BigDecimal vat;
    private BigDecimal price;
    private BigDecimal wholesalePrice;
    private BigDecimal oldPrice;
    private Integer status;
    private Object multiUnitItems;
    private Object comboItems;
    private Long categoryId;
    private Long internalCategoryId;
    private Long brandId;
    private Double shippingWeight;
    private String unit;
    private Integer length;
    private Integer width;
    private Integer height;
    private Long countryId;
    private Long warrantyAddress;
    private String warrantyPhone;
    private Long warranty;
    private String warrantyContent;
    private Integer firstRemain;
    private Integer importType;
    private Long depotId;
    private String supplierName;
    private Long supplierId;
    private Integer checkcopyImg;
    private Integer attributeCombinated;
    private String metaTitle;
    private String metaDescription;
    private Object metaKeywords;
    private Object tags;
    @JsonProperty("tag-suggest")
    private Object tagSuggest;

    public String getNuctk() {
        return nuctk;
    }

    public void setNuctk(String nuctk) {
        this.nuctk = nuctk;
    }

    public Long getStoreId() {
        return storeId;
    }

    public void setStoreId(Long storeId) {
        this.storeId = storeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getTypeId() {
        return typeId;
    }

    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    public String getParentIdName() {
        return parentIdName;
    }

    public void setParentIdName(String parentIdName) {
        this.parentIdName = parentIdName;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    public BigDecimal getImportPrice() {
        return importPrice;
    }

    public void setImportPrice(BigDecimal importPrice) {
        this.importPrice = importPrice;
    }

    public BigDecimal getVat() {
        return vat;
    }

    public void setVat(BigDecimal vat) {
        this.vat = vat;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getWholesalePrice() {
        return wholesalePrice;
    }

    public void setWholesalePrice(BigDecimal wholesalePrice) {
        this.wholesalePrice = wholesalePrice;
    }

    public BigDecimal getOldPrice() {
        return oldPrice;
    }

    public void setOldPrice(BigDecimal oldPrice) {
        this.oldPrice = oldPrice;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Object getMultiUnitItems() {
        return multiUnitItems;
    }

    public void setMultiUnitItems(Object multiUnitItems) {
        this.multiUnitItems = multiUnitItems;
    }

    public Object getComboItems() {
        return comboItems;
    }

    public void setComboItems(Object comboItems) {
        this.comboItems = comboItems;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public Long getInternalCategoryId() {
        return internalCategoryId;
    }

    public void setInternalCategoryId(Long internalCategoryId) {
        this.internalCategoryId = internalCategoryId;
    }

    public Long getBrandId() {
        return brandId;
    }

    public void setBrandId(Long brandId) {
        this.brandId = brandId;
    }

    public Double getShippingWeight() {
        return shippingWeight;
    }

    public void setShippingWeight(Double shippingWeight) {
        this.shippingWeight = shippingWeight;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Long getCountryId() {
        return countryId;
    }

    public void setCountryId(Long countryId) {
        this.countryId = countryId;
    }

    public Long getWarrantyAddress() {
        return warrantyAddress;
    }

    public void setWarrantyAddress(Long warrantyAddress) {
        this.warrantyAddress = warrantyAddress;
    }

    public String getWarrantyPhone() {
        return warrantyPhone;
    }

    public void setWarrantyPhone(String warrantyPhone) {
        this.warrantyPhone = warrantyPhone;
    }

    public Long getWarranty() {
        return warranty;
    }

    public void setWarranty(Long warranty) {
        this.warranty = warranty;
    }

    public String getWarrantyContent() {
        return warrantyContent;
    }

    public void setWarrantyContent(String warrantyContent) {
        this.warrantyContent = warrantyContent;
    }

    public Integer getFirstRemain() {
        return firstRemain;
    }

    public void setFirstRemain(Integer firstRemain) {
        this.firstRemain = firstRemain;
    }

    public Integer getImportType() {
        return importType;
    }

    public void setImportType(Integer importType) {
        this.importType = importType;
    }

    public Long getDepotId() {
        return depotId;
    }

    public void setDepotId(Long depotId) {
        this.depotId = depotId;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public Long getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }

    public Integer getCheckcopyImg() {
        return checkcopyImg;
    }

    public void setCheckcopyImg(Integer checkcopyImg) {
        this.checkcopyImg = checkcopyImg;
    }

    public Integer getAttributeCombinated() {
        return attributeCombinated;
    }

    public void setAttributeCombinated(Integer attributeCombinated) {
        this.attributeCombinated = attributeCombinated;
    }

    public String getMetaTitle() {
        return metaTitle;
    }

    public void setMetaTitle(String metaTitle) {
        this.metaTitle = metaTitle;
    }

    public String getMetaDescription() {
        return metaDescription;
    }

    public void setMetaDescription(String metaDescription) {
        this.metaDescription = metaDescription;
    }

    public Object getMetaKeywords() {
        return metaKeywords;
    }

    public void setMetaKeywords(Object metaKeywords) {
        this.metaKeywords = metaKeywords;
    }

    public Object getTags() {
        return tags;
    }

    public void setTags(Object tags) {
        this.tags = tags;
    }

    public Object getTagSuggest() {
        return tagSuggest;
    }

    public void setTagSuggest(Object tagSuggest) {
        this.tagSuggest = tagSuggest;
    }

    @Override
    public String toString() {
        return "CreateNhanhProductRequest{" +
                "nuctk='" + nuctk + '\'' +
                ", storeId=" + storeId +
                ", name='" + name + '\'' +
                ", typeId=" + typeId +
                ", parentIdName='" + parentIdName + '\'' +
                ", parentId=" + parentId +
                ", code='" + code + '\'' +
                ", barcode='" + barcode + '\'' +
                ", importPrice=" + importPrice +
                ", vat=" + vat +
                ", price=" + price +
                ", wholesalePrice=" + wholesalePrice +
                ", oldPrice=" + oldPrice +
                ", status=" + status +
                ", multiUnitItems=" + multiUnitItems +
                ", comboItems=" + comboItems +
                ", categoryId=" + categoryId +
                ", internalCategoryId=" + internalCategoryId +
                ", brandId=" + brandId +
                ", shippingWeight=" + shippingWeight +
                ", unit='" + unit + '\'' +
                ", length=" + length +
                ", width=" + width +
                ", height=" + height +
                ", countryId=" + countryId +
                ", warrantyAddress=" + warrantyAddress +
                ", warrantyPhone='" + warrantyPhone + '\'' +
                ", warranty=" + warranty +
                ", warrantyContent='" + warrantyContent + '\'' +
                ", firstRemain=" + firstRemain +
                ", importType=" + importType +
                ", depotId=" + depotId +
                ", supplierName='" + supplierName + '\'' +
                ", supplierId=" + supplierId +
                ", checkcopyImg=" + checkcopyImg +
                ", attributeCombinated=" + attributeCombinated +
                ", metaTitle='" + metaTitle + '\'' +
                ", metaDescription='" + metaDescription + '\'' +
                ", metaKeywords=" + metaKeywords +
                ", tags=" + tags +
                ", tagSuggest=" + tagSuggest +
                '}';
    }

    //    private Multi imageUpload;
}

I want to add same nuctk in every request in the body. But the request uses the formData I can't convert this to object in interceptor.

My client:

package com.example.demo.client;

import com.example.demo.config.NhanhPageFeignClientInterceptor;
import com.example.demo.request.CreateNhanhProductRequest;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(
        value = "nhanhPage",
        url = "https://nhanh.vn/product",
        configuration = NhanhPageFeignClientInterceptor.class
)
public interface NhanhProductV1Client {

    @PostMapping(value = "/item/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String createProduct(@RequestBody CreateNhanhProductRequest request);

}

This is my interceptor:

package com.example.demo.config;

import com.example.demo.request.CreateNhanhProductRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.RequestInterceptor;
import feign.form.FormData;
import feign.form.FormEncoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.io.IOException;

public class NhanhPageFeignClientInterceptor {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            String bodyTemplate = requestTemplate.bodyTemplate();
            byte[] body = requestTemplate.body();
            FormData formData = new FormData(MediaType.MULTIPART_FORM_DATA_VALUE, "data" , body);

            requestTemplate.header(HttpHeaders.COOKIE, "Some cookie");
        };
    }

}

How to change the body in feign client interceptor ?

Thank!

0

There are 0 best solutions below