解决后Using Springfox to document jax-rs services in a Spring app ,我现在发现 SpringFox 的 JSON 回复没有显示任何 API:
{
"swagger": "2.0",
"info": {
"description": "Some description",
"version": "1.0",
"title": "My awesome API",
"contact": {
"name": "my-email@domain.org"
},
"license": {}
},
"host": "localhost:9090",
"basePath": "/myapp"
}
这是 springfox-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON" />
<bean class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider" />
<bean class="com.wordnik.swagger.jaxrs.listing.ResourceListingProvider" />
</beans>
这是在属性文件中:
swagger.resourcePackage=org.myapp
Swagger 被配置为使用反射 jax-rs 扫描器查找实现类:
@Component
public class SwaggerConfiguration {
@Value("${swagger.resourcePackage}")
private String resourcePackage;
@PostConstruct
public void init() {
ReflectiveJaxrsScanner scanner = new ReflectiveJaxrsScanner();
scanner.setResourcePackage(resourcePackage);
ScannerFactory.setScanner(scanner);
ClassReaders.setReader(new DefaultJaxrsApiReader());
SwaggerConfig config = ConfigFactory.config();
config.setApiVersion(apiVersion);
config.setBasePath(basePath);
}
public String getResourcePackage() {
return resourcePackage;
}
public void setResourcePackage(String resourcePackage) {
this.resourcePackage = resourcePackage;
}
}
这是文档配置:
@Configuration
@EnableSwagger2
public class ApiDocumentationConfiguration {
@Bean
public Docket documentation() {
System.out.println("=========================================== Initializing Swagger");
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.apiInfo(metadata());
}
@Bean
public UiConfiguration uiConfig() {
return UiConfiguration.DEFAULT;
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("My awesome API")
.description("Some description")
.version("1.0")
.contact("my-email@domain.org")
.build();
}
}
这是一个带有 api 注释的示例类:
@Api(value = "activity")
@Service
@Path("api/activity")
@Produces({ MediaType.APPLICATION_JSON })
public class ActivityService {
@Autowired
private CommandExecutor commandExecutor;
@Autowired
private FetchActivityCommand fetchActivityCommand;
@ApiOperation(value = "Fetch logged-in user's activity", httpMethod = "GET", response = Response.class)
@GET
@Path("/mine")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
@Authorization(rejectionMessage = Properties.Authorization.NOT_LOGGED_IN_MESSAGE_PREFIX + "view your activities.")
public List<Activity> listMyActivities(@Context HttpServletResponse response, @Context HttpServletRequest request) throws IOException {
return buildActivityList(response, (UUID) request.getSession().getAttribute(Properties.Session.SESSION_KEY_USER_GUID));
}
...
}
为什么不公开 API?使用 wordnik swagger 库会解决这个问题,还是会改进解决方案?
最佳答案
默认情况下,SpringFox 将记录使用 Spring MVC 实现的 REST 服务 - 这就像添加 @EnableSwagger2 注释一样简单
@SpringBootApplication
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我设法让 SpringFox 与 JAX-RS 一起工作 起初,我添加了一些 swagger 依赖项以及 SpringFox:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-jersey")
compile("io.springfox:springfox-swagger-ui:2.4.0")
compile("io.springfox:springfox-swagger2:2.4.0")
compile("io.swagger:swagger-jersey2-jaxrs:1.5.8")
testCompile("junit:junit")
}
然后我在我的 spring-boot 应用程序中启用了带有 Swagger 的 JAX-RS (Jersey):
@Component
@ApplicationPath("/api")
public static class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
BeanConfig swaggerConfig = new BeanConfig();
swaggerConfig.setBasePath("/api");
SwaggerConfigLocator.getInstance().putConfig(SwaggerContextService.CONFIG_ID_DEFAULT, swaggerConfig);
packages(getClass().getPackage().getName(), ApiListingResource.class.getPackage().getName());
}
}
请注意,所有 JAX-RS 端点都将在 /api 上下文下 - 否则会与 Spring-MVC 调度程序冲突
最后,我们应该将为 Jersey 端点生成的 swagger json 添加到 Springfox:
@Component
@Primary
public static class CombinedSwaggerResourcesProvider implements SwaggerResourcesProvider {
@Resource
private InMemorySwaggerResourcesProvider inMemorySwaggerResourcesProvider;
@Override
public List<SwaggerResource> get() {
SwaggerResource jerseySwaggerResource = new SwaggerResource();
jerseySwaggerResource.setLocation("/api/swagger.json");
jerseySwaggerResource.setSwaggerVersion("2.0");
jerseySwaggerResource.setName("Jersey");
return Stream.concat(Stream.of(jerseySwaggerResource), inMemorySwaggerResourcesProvider.get().stream()).collect(Collectors.toList());
}
}
就是这样!现在,您的 JAX-RS 端点将由 Swagger 记录。 我使用了以下示例端点:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Component
@Path("/hello")
@Api
public class Endpoint {
@GET
@ApiOperation("Get message")
@Produces(MediaType.TEXT_PLAIN)
public String message() {
return "Hello";
}
}
现在,当您启动服务器并转到 http://localhost:8080/swagger-ui.html 时您将看到 JAX-RS 端点的文档。 使用页面顶部的combobox切换到Spring MVC端点的文档
关于java - SpringFox 找不到 jax-rs 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32877898/
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.
Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur