2015-09-28 43 views
1

我有几个现有的@RestController s。访问这些控制器的路径是例如:如何通过条件委托给RestController?

localhost/first/test 
localhost/second/test 

代码:

@RestController 
@RequestMapping("/first") 
public class MyRestController1 { 
    @ResponseStatus(HttpStatus.OK) 
    @RequestMapping(value = "/test", method = RequestMethod.GET) 
    public void test(@Valid RestParameters p) { 
     //... 
    } 
} 

@RestController 
@RequestMapping("/second") 
public class MyRestController2 { 

} 

问题:是否有可能赶上不同的网址,和委托给这些控制器包括@Valid其余参数的自动验证?

例如:localhost?param=first。是否有可能把这个委托给localhost/first/test

此外我想复制完整的querystring并将其发送到适当的restcontroller。访问/first/second时,查询字符串会有所不同,并且可能有不同的参数。

回答

0

步骤1) 创建一个捕获@Path("/")的类。我们称之为“Class1”

步骤2) 在Class1中创建方法,但不要更改此方法的@Path("")。确保它适当地捕获@QueryString("param") String firstOrLast。我们称之为“Method1”

步骤3) 现在当您访问localhost?param=first时,Class1.Method1()被调用。因此,在这种方法中,你可以建立一些逻辑(例如if (firstOrLast.equals("first")) { // call method_x },其中method_x是你调用它的相关委托方法。

+0

是的,但是如果我委托给'myRestController1.test(params)'我想确保这个get自动验证'@ Valid'和所有其他'spring'东西 – membersound

+0

您是否正在验证'Class1.Method1()'中每个条件的相同对象?如果为true,那么您可以复制/移动'@ Valid'东西到授权之前的'Class1.Method1()'... – Niccaman

+0

不,我有不同的验证约束条件,因为'get-query'字符串在控制器之间会有所不同。 – membersound