我如何使用@GetMapping在Controller中以相同的方法使用不同的URL?

时间:2019-03-29 14:57:25

标签: spring-mvc get-mapping

它无法同时在两个URL上运行

@GetMapping(value= {"/durationtrend/{moduleId}","/durationtrend/{moduleId}/{records}"},produces=MediaType.APPLICATION_JSON_VALUE)
public List<ExecutionDurationResource> getExecutionDurationByModuleId(@PathVariable("moduleId") Integer moduleId,@PathVariable("records") Integer records) {    
    return executionDurationService.getExecutionDuration(moduleId,records); 
}

http://localhost:8080/seleniumexecutiontrending/reports/durationtrend/427->不通话。 http://localhost:8080/seleniumexecutiontrending/reports/durationtrend/427/7-->执行。

我想用同一方法执行两者

3 个答案:

答案 0 :(得分:0)

 @GetMapping(value= {"/durationtrend/{moduleId}","/durationtrend/{moduleId}/{records}"},produces= MediaType.APPLICATION_JSON_VALUE)
public List getExecutionDurationByModuleId(HttpServletRequest request) {
    Map map= (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    Integer moduleId=null,records=null;
    if(map!=null && map.get("moduleId")!=null){
        moduleId = Integer.valueOf(map.get("moduleId").toString());
    }
    if(map!=null && map.get("records")!=null){
        records = Integer.valueOf(map.get("records").toString());
    }
    System.out.println("moduleId:"+moduleId);
    System.out.println("records:"+records);
    //then you use modules and records , just judge  whether they are null?
    return executionDurationService.getExecutionDuration(moduleId,records);
}

我尝试了上面的有效代码。试试吧,希望对您有所帮助!

答案 1 :(得分:0)

这是您的问题

@PathVariable("moduleId") Integer moduleId,@PathVariable("records") Integer records

您的两个URL都需要 moduleId records 参数,在第一个URL "/durationtrend/{moduleId}"中,您没有records参数,这就是您拥有

  

org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException   解决了[org.springframework.web.bind.MissingPathVariableException:   类型的方法参数缺少URI模板变量“ records”   整数]

此错误

实际上,您可以通过多种方式实现此目标,例如使用HttpServletRequest等,但是请记住您正在使用spring,并且需要使用spring框架简化这些事情。

我建议的简单方法是使用两个单独的控制器并简化操作。

@GetMapping("/durationtrend/{moduleId}")
public void getExecutionDurationByModuleId(@PathVariable("moduleId") Integer moduleId) {
    return executionDurationService.getExecutionDuration(moduleId);
    System.out.println(moduleId);
}

@GetMapping("/durationtrend/{moduleId}/{records}")
public void getExecutionDurationByRecords(@PathVariable("moduleId") Integer moduleId, @PathVariable("records") Integer records) {
    return executionDurationService.getExecutionDuration(moduleId, records);
    System.out.println(moduleId);
    System.out.println(records);
}

这很容易理解,您可以在服务类中创建getExecutionDuration(moduleId)方法,并可以轻松地绕开它...

希望这对您有帮助...

答案 2 :(得分:0)

@GetMapping(value= "/module-execution-trend",produces=MediaType.APPLICATION_JSON_VALUE) 
     public List<ExecutionResource> getExecutionResult(@RequestParam("moduleId") Integer moduleId,@RequestParam(name="records",required=false,defaultValue="10")  Integer records ) 
     {
        System.out.println(moduleId); 
        System.out.println(records); 
         return executionService.getModuleExecutionResult(moduleId,records);
     }