Spring Boot array of JSON strings as a param of x-www-form-urlencoded request content

Viewed 570

As far as I know, Spring Boot has two ways of passing arrays in request parameters:

  1. comma separated values, ex: numbers=1,2,3,4,5
  2. repeated parameter name, ex: numbers=1&numbers=2&numbers=3&numbers=4&numbers=5

In my example below, it first tries to convert an array using method 1. then after exception it successfully convert it using method 2.


My questions are:

is there any way to tell Spring Boot to use only method 2.?

Alternatively is there any way to escape commas in JSON string passed as request parameter of x-www-form-urlencoded content type?


DemoObject.class:

public class DemoObject {
    
    private String name;
    private String description;
    private Integer count;

    //Constructor, Getters and Setters

}

DemoController.class:

@RestController
public class DemoController {
    
    @PostMapping("test")
    String test(@RequestParam List<DemoObject> objects, @RequestParam Integer number) throws JsonProcessingException {
        return "["+number +"] " +om.writerWithDefaultPrettyPrinter().writeValueAsString(objects);
    }
    
}

DemoConverter.class:

@Component
public class DemoConverter implements GenericConverter {

    private final ObjectMapper om = new ObjectMapper();
    
    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        Set<ConvertiblePair> convertibles = new HashSet<>();
        convertibles.add(new ConvertiblePair(String.class, DemoObject.class));
        return convertibles;
    }

    @Override
    public Object convert(Object o, TypeDescriptor sourceType, TypeDescriptor targetType) {
        try {
            return om.readValue((String) o, targetType.getType());
        } catch (JsonProcessingException ex) {
            Logger.getLogger(DemoConverter.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("Conversion failed");
        }
    }

}

Sample request 1.:

POST http://localhost:8080/test  
Body x-www-form-urlencoded  
objects:{"name":"test","description":"AAA"}
number:123

Spring Boot throws an exception:

com.fasterxml.jackson.core.io.JsonEOFException: 
Unexpected end-of-input: expected close marker for Object 
(start marker at [Source: (String)"{"name":"test""; line: 1, column: 1])

But result is correct with status 200 code.


Sample request 2.:

POST http://localhost:8080/test  
Body x-www-form-urlencoded  
objects:{"name":"test","description":"AAA"}  
objects:{"name":"test2","description":"BBB"}  
number:123

No exception, status 200, correct result.


Any help appreciated.

0 Answers
Related