您的位置:首页 > 其它

RESTful风格的Web服务框架 Swagger

2016-08-20 12:15 453 查看
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

创建工程





1.REST API

import javax.ws.rs.Consumes;

import javax.ws.rs.GET;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

import com.wordnik.swagger.annotations.Api;

import com.wordnik.swagger.annotations.ApiOperation;

@Path(“/user”)

@Api(value = “/user”, description = “User REST for Integration Testing”)

public interface UserService {

@GET

@Path(“/getUser/{username}”)

@Consumes(MediaType.APPLICATION_JSON)

@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = “Get user details”, response = User.class)

public User getUser(@PathParam(“username”) String username);

@POST

@Path(“/saveUser”)

@Consumes(MediaType.APPLICATION_JSON)

@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = “Save user details”, response = User.class)

public User getUser(User user);

}

2.Swagger Config

<!– Swagger API listing resource –>

<bean id=“swaggerResourceJSON”

class=“com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON” />

<!– Swagger writers –>

<bean id=“resourceWriter”

class=“com.wordnik.swagger.jaxrs.listing.ResourceListingProvider” />

<bean id=“apiWriter”

class=“com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider” />

<jaxrs:server address=“/sw” id=“swagger”>

<jaxrs:serviceBeans>

<ref bean=“swaggerResourceJSON” />

</jaxrs:serviceBeans>

<jaxrs:providers>

<ref bean=“resourceWriter” />

<ref bean=“apiWriter” />

</jaxrs:providers>

</jaxrs:server>

<bean id=“swaggerConfig”>

<property name=“resourcePackage” value=“com.xymiyue ” />

<property name=“version” value=“2.0″ />

<property name=“basePath” value=“http://localhost:8080/SwaggerUI-Integration/rest” />

<property name=“title” value=“Swagger UI Integration Sample” />

<property name=“description”

value=“Swagger UI Integration Sample for demonstrating its working.” />

<property name=“contact” value=“hi@wangbaocai.cn” />

<property name=“scan” value=“true” />

</bean>

3.Swagger UI





UI页面参考 https://github.com/Duttor/swagger-ui
参考示例工程:

https://github.com/saurabh29july/SwaggerUI-Integration
http://wangbaocai.cn/?p=1204
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: