问题
As the title suggests I have a Controller endpoint I'm trying to document. The endpoint can take 2 parameters both are optional. When it goes to generate the code the parameter is duplicated.
Controller
@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");
}
}
Documentation
@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());
}
And here is the generated snippet for the http-request.adoc
[source,http,options="nowrap"]
----
GET /bitbucket/project?key=KEY&key=KEY HTTP/1.1
Host: localhost:8080
----
Running Spring Boot 1.4.0 and Rest Docs 1.1.1
回答1:
This was due to a bug in 1.1.1. Upgrading to 1.1.2 will fix the problem.
来源:https://stackoverflow.com/questions/39537573/duplicate-request-parameters-being-recorded-in-the-snippet