正如标题所示,我有一个控制器端点,我正在尝试记录。端点可以采用 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
----
运行 Spring Boot 1.4.0 和 Rest Docs 1.1.1