Oleksandr
Oleksandr

Reputation: 3744

How to convert one request param into several request params in Spring before calling a controller?

The client sends some data in one request param like:

example.com/test?myparam=some123data

I would like to convert myparam into several other params and call a necessary controller with such parameters. Like this one:

@RequestMapping(value = "/test")
public @ResponseBody MyObject test(
    @RequestParam(value = "prefix") String prefix, // some
    @RequestParam(value = "number") int number, // 123
    @RequestParam(value = "suffix") String suffix)  //data
{ ... }

It is possible to put some custom converter for such situation?

Upvotes: 0

Views: 126

Answers (2)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

I'm not really sure if it can be made wit request params. Instead, you could use path variables with regular expressions in the following way:

@RequestMapping(value = "/test/{prefix:[a-z]+}{number:[0-9]+}{suffix:[a-z]+}")
public @ResponseBody MyObject test(
    @PathVariable(value = "prefix") String prefix, // some
    @PathVariable(value = "number") int number, // 123
    @PathVariable(value = "suffix") String suffix)  //data
{ ... }

In this case your request URL will look like this:

example.com/test/some123data

Upvotes: 2

Max Farsikov
Max Farsikov

Reputation: 2763

You can try to implement your own argument resolver, here an example

Upvotes: 0

Related Questions