Spring boot: RequestMapping

I have three REST API methods:

@RequestMapping(value = "/{name1}", method = RequestMethod.GET)
    public Object retrieve(@PathVariable String name1) throws UnsupportedEncodingException {
        return configService.getConfig("frontend", name1);
    }

@RequestMapping(value = "/{name1}/{name2}", method = RequestMethod.GET)
public Object retrieve(@PathVariable String name1, @PathVariable String name2) throws UnsupportedEncodingException {
    return configService.getConfig("frontend", name1, name2);
}

@RequestMapping(value = "/{name1}/{name2}/{name3}", method = RequestMethod.GET)
public Object retrieve(@PathVariable String name1, @PathVariable String name2, @PathVariable String name3) {
    return configService.getConfig("frontend", name1, name2,name3);
}

The getConfig method is configured to accept several parameters, such as:

 public Object getConfig(String... names) {

My question is: is it possible to execute the above RequestMapping using only one / RequestMapping method?

Thank.

+4
source share
4 answers

Simple approach

You can use /**in your mapping to grab any url and then extract all parameters from the mapping path. Spring has a constant that allows you to get the path from an HTTP request. You just need to remove the unnecessary part of the display and split the rest to get a list of options.

import org.springframework.web.servlet.HandlerMapping;

@RestController
@RequestMapping("/somePath")
public class SomeController {

    @RequestMapping(value = "/**", method = RequestMethod.GET)
    public Object retrieve(HttpServletRequest request) {
        String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
        String[] names = path.substring("/somePath/".length()).split("/");
        return configService.getConfig("frontend", names);
    }

}

Best approach

, . .

http://yourapp.com/somePath?name=value1&name=value2

:

@RequestMapping(method = RequestMethod.GET)
public Object retrieve(@RequestParam("name") String[] names) {
    return configService.getConfig("frontend", names);
}
+3

, @RequestParam POST .

@RequestMapping(name = "/hi", method = RequestMethod.POST)
@ResponseBody
public String test(@RequestParam("test") String[] test){

    return "result";
}

:

enter image description here

,

REST , : " ?". , - /config/frontend, / HTTP-

+2

request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE), , .

+1
source

This should work:

@SpringBootApplication
@Controller
public class DemoApplication {


public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

@RequestMapping(value ={"/{name1}","/{name1}/{name2}","/{name1}/{name2}/{name3}"})
public @ResponseBody String testMethod(
        @PathVariable Map<String,String> pathvariables)
{
    return test(pathvariables.values().toArray(new String[0]));
}

private String test (String... args) {
    return Arrays.toString(args);
}

}

+1
source

Source: https://habr.com/ru/post/1662522/


All Articles