0

We have a large application built on Spring 3.

We're using this example to build a report of all the endpoints of the application.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
public class EndpointDocController {
 private final RequestMappingHandlerMapping handlerMapping;

 @Autowired
 public EndpointDocController(RequestMappingHandlerMapping handlerMapping) {
  this.handlerMapping = handlerMapping;
 }

 @RequestMapping(value="/endpointdoc", method=RequestMethod.GET)
 public void show(Model model) {
  model.addAttribute("handlerMethods", this.handlerMapping.getHandlerMethods());
 } 
}

It would be really helpful if this report could include documentation about the endpoint.

My question is: In Spring 3 Web - is it possible to embed metadata as documentation in the RequestMapping annotation?

Community
  • 1
  • 1
hawkeye
  • 34,745
  • 30
  • 150
  • 304

1 Answers1

0

If you look at the javadoc you see that there are no additional fields for that annotation to put in documentation, so straight off, no.

However, you could create your own annotation(@DocumentedRequestMapping) inheriting from the @RequestMapping which has a field documentation. Then use the handlermapping to get to the method, get it's annotations, get it's field, and that might help you. Something like

Map<T,HandlerMethod> map = this.handlerMapping.getHandlerMethods();
HandlerMethod handlerMethod = map.get('some T');
Method annotatedMethod = handlerMethod.getMethod();
Annotation documentedRequestMappingAnnotation = AnnotationUtils.findAnnotation(annotatedMethod, DocumentedRequestMapping.class);
Map<String, Object> annotationValues = Annotationutils.getAnnotationAttributes(documentedRequestMappingAnnotation);
annotationValues.get("documentation");

Mind, this is totally untested

Koos Gadellaa
  • 1,220
  • 7
  • 17