2016-09-16 42 views
0

正如標題所示,我有一個控制器端點正在嘗試記錄。端點可以採用2個參數,都是可選的。當它生成代碼時,參數被複制。記錄在片段中的重複請求參數

控制器

@RequestMapping("/bitbucket/project") 
    public Project findProject(@RequestParam(value = "key", required = false) String key, 
      @RequestParam(value = "name", required = false) String name) 
    { 
      if (!StringUtils.isEmpty(key)) 
      { 
        try 
        { 
          Project project = projectService.findProjectByKey(key); 
          if (project == null) 
          { 
            throw new NotFoundException("Project not found"); 
          } 
          else 
          { 
            return project; 
          } 
        } 
        catch (IOException e) 
        { 
          LOG.error(e.getMessage(), e); 
          throw new ServerException(e.getMessage()); 
        } 
      } 
      else if (!StringUtils.isEmpty(name)) 
      { 
        try 
        { 
          Project project = projectService.findProjectByName(name); 
          if (project == null) 
          { 
            throw new NotFoundException("Project not found"); 
          } 
          else 
          { 
            return project; 
          } 
        } 
        catch (IOException e) 
        { 
          LOG.error(e.getMessage(), e); 
          throw new ServerException(e.getMessage()); 
        } 
      } 
      else 
      { 
        throw new BadRequestException("Project not found"); 
      } 
    } 

文檔

@Rule 
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(
      "target/generated-snippets"); 

    private RestDocumentationResultHandler document; 

    @Autowired 
    private WebApplicationContext context; 

    private MockMvc mockMvc; 

    @Before 
    public void setUp() 
    { 
      this.document = document("{method-name}", preprocessRequest(prettyPrint()), 
        preprocessResponse(prettyPrint())); 

      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) 
        .apply(documentationConfiguration(this.restDocumentation)) 
        .alwaysDo(this.document).build(); 
    } 

    @Test 
    public void findProjectKey() throws Exception 
    { 
      String projectKey = "KEY"; 
      when(projectService.findProjectByKey(anyString())) 
        .thenReturn(createProject(projectKey, null, false)); 

      getMockMvc().perform(get("/bitbucket/project").param("key", projectKey)) 
        .andExpect(status().isOk()); 
    } 

這裏是爲HTTP-request.adoc

[source,http,options="nowrap"] 
---- 
GET /bitbucket/project?key=KEY&key=KEY HTTP/1.1 
Host: localhost:8080 

---- 

運行春季啓動1.4.0生成的代碼段和休息 Docs 1.1.1

回答

1

這是由於1.1.1中的bug。升級到1.1.2將解決問題。

+0

更新至1.1.2後仍然存在問題 – ndrone

+0

您確定您使用的是spring-restdocs-core的1.1.2版嗎?當你使用Spring Boot時,你應該在你的pom.xml或build.gradle中重寫'spring-restdocs.version'。 –

+0

我會對它進行測試並回復你。我只改變了我的pom.xml – ndrone