Как я могу использовать разные URL с тем же методом в контроллере с @GetMapping? - PullRequest
0 голосов
/ 29 марта 2019

Невозможно запустить оба 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-->it выполняется.

Я хочу выполнить оба в одном методе

Ответы [ 3 ]

0 голосов
/ 30 марта 2019

здесь ваша проблема

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

вам нужны moduleId и записи параметров для обоих ваших URL, в первом URL "/durationtrend/{moduleId}" у вас не былоПараметр records, поэтому у вас есть

org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException Resolved [org.springframework.web.bind.MissingPathVariableException: отсутствует переменная шаблона URI для метода «records» для записейтипа Integer]

эта ошибка

На самом деле, вы можете достичь этой цели разными способами, например, используя HttpServletRequest и другие вещи, но помните, что вы используете spring и вам нужно упроститьВещи, использующие Spring Framework.

Простой способ, который я предлагаю, - использовать два отдельных контроллера и упростить вещи.

@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) в своем классе обслуживания и легко обойти его ...

надеюсь, что это полезно ...

0 голосов
/ 01 апреля 2019
@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);
     }
0 голосов
/ 30 марта 2019
 @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);
}

Я попробовал код выше, который работает. Попробуйте, надеемся, что вам помогут!

...