2015-09-24 32 views
0

在过去,当在应用程序启动时在JVM中使用Spring REST时,在容器完全引导之前,Spring MVC模块会输出它是所有RESTful API的列表配置为暴露?我从那以后无法在春季的更新版本中重现这一点?我意识到如果有人能够保留你的日志,那么这样做会带来安全风险,但我需要尽快通过我们的系统公开的REST调用列表。如果有人有任何建议,我将不胜感激。生成公开的RESTful URI的列表

谢谢,
马克。

+0

也许你想为“REST服务消费者”没有,不希望有访问源代码清单。在这种情况下,您可以使用http://mvnrepository.com/artifact/org.codehaus.enunciate/maven-enunciate-plugin。您只需添加一些注释+注释,它将生成一个HTML文件(+其他资源)作为文档页面。这可能是有用的:http://stackoverflow.com/questions/11939542/how-do-you-document-a-rest-api –

回答

2

我不确定它是否正是您正在寻找的东西 - 我们想出了一个IndexController,它呈现的响应包含所有使用HATEOAS风格的ExposesResourceFor注解的控制器。因此,我们为每个控制器获取集合资源的入口点。

你可以用你的控制器用RestController或RequestMapping注解做类似的事情。

@RestController 
@RequestMapping(path = "/", produces = { "application/hal+json", "application/json" }) 
public class IndexController { 
    private final Set<Class<?>> entitiesWithController; 
    private EntityLinks entityLinks; 
    private RelProvider relProvider; 

    @Autowired 
    public IndexController(ListableBeanFactory beanFactory, EntityLinks entityLinks, RelProvider relProvider) { 
     this.entityLinks = entityLinks; 
     this.relProvider = relProvider; 
     Map<String, Object> beansWithExposesResourceForAnnotation = beanFactory.getBeansWithAnnotation(ExposesResourceFor.class); 
     entitiesWithController = beansWithExposesResourceForAnnotation.values().stream() 
       .map(o -> o.getClass().getAnnotation(ExposesResourceFor.class).value()).collect(Collectors.toSet()); 
    } 

    @RequestMapping(method = GET) 
    public ResponseEntity<ControllerLinksResource> getControllerLinks() { 
     ControllerLinksResource controllerLinksResource = new ControllerLinksResource(); 

     entitiesWithController.forEach(entityClass -> controllerLinksResource // 
       .add(entityLinks.linkToCollectionResource(entityClass) // 
         .withRel(relProvider.getCollectionResourceRelFor(entityClass)))); 

     return ResponseEntity.ok(controllerLinksResource); 
    } 
} 
+0

感谢百万Zaddo,似乎让你回到每个控制器注释类在JVM?唉,我正在寻找所有暴露的URI的列表。在spring完成接线之前,我确实已经看到了这样的打印,并且tomcat服务器在catalina.log中启动。我真的很想知道如何将它重新打开。 –

+1

嘿 - 如果它帮助你仍然可以upvote我anwer ;-) - 上面的代码搜索控件注释ExposesResourceFor - 但你可以改变它'@ RestController'或'@ RequestMapping'。你的意思是这样的日志条目? - 'Mapped“{[/system/metrics/{name:.*}],methods=[GET]}”public class java.lang.Object ...' - 当我的spring启动应用程序启动时我会看到它们 - 输出来自'org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping' –

+0

宾果这正是我要找的Mathias!道歉现在我没有意识到我没有提高你的答案!我如何将答复标记为答案? –

1

基本上你需要REST API文档,你可以使用任何的以下REST API文档的框架。

Swagger

apiDoc

JsonDoc

+0

非常感谢Lovababu唉,这仍然意味着通过每个控制器拖网和手动更新它们。这绝对是我将来要加入的东西,但现在我需要一种找到列表的方式,而不必触摸每个控制器。我们有很多:D –