-1

I have POJO class that represents list of request params.

Currently it uses field names, but I need to set them to kebab-case naming. For example: List<Product> productIds = new ArrayList<>() should accept request param product-ids instead of productIds.

Using directly @RequestParam I can do @RequestParam(name="product-ids"), but I need it using binded POJO.

@JsonProperty or @JsonNaming(KebabCaseStrategy.class) don't work on that fields, I think because it's not a serialiazable object, but just a request param container.

Examples in How to customize parameter names when binding Spring MVC command objects? dont work for me.

Exception:

|  paramNameProcessor (field private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter com.example.jpastudy.config.ParamNameProcessor.requestMappingHandlerAdapter)
↑     ↓
|  requestMappingHandlerAdapter defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
└─────┘

How could I resolve that?

andrew17
  • 851
  • 2
  • 10
  • 25
  • So you're using it like this? `void request(BoundedPojo boundedPojo)` – Idan Elhalwani May 29 '22 at 16:38
  • Yes, I pass Pojo to controller method argument @IdanElhalwani – andrew17 May 29 '22 at 16:39
  • From what I remember it uses the name of the setter to determine this. Is switching to RequestBody an option for you? Alternatively look here: [How to customize parameter names when binding Spring MVC command objects?](https://stackoverflow.com/questions/8986593/how-to-customize-parameter-names-when-binding-spring-mvc-command-objects) – Idan Elhalwani May 29 '22 at 17:01
  • No I need request param, not json body. I tried that examples from link, all of them are not valid, not working... There is mistakes with beans configuration, etc. – andrew17 May 29 '22 at 17:12
  • @IdanElhalwani any idea? – andrew17 May 29 '22 at 17:37

2 Answers2

0

I solved the problem adding property:

spring.main.allow-circular-references=true

BUT I am not sure that it's a correct solution.

andrew17
  • 851
  • 2
  • 10
  • 25
0

I use this as a workaround.

import java.beans.ConstructorProperties;

public class RequestParamsDto {

    private List<Product> productIds;

    @ConstructorProperties("product-ids")
    public RequestParamsDto(List<Product> productIds) {
        this.productIds = productIds;
    }

    public List<Product> getProductIds() {
        return this.productIds;
    }

    public void setProductIds(List<Product> productIds) {
        this.productIds = productIds;
    }
}
kekhuay
  • 377
  • 2
  • 3
  • 17