Jfinal集成swagger的最简单方式,支持response

新建一个Controller命名叫SwaggerController内容如下 

package com.operation.controller;

import com.google.gson.Gson;
import com.ideabobo.swagger.util.StringUtil;
import com.jfinal.core.Controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.models.Contact;
import io.swagger.models.Info;
import io.swagger.models.License;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.Scheme;
import io.swagger.models.Swagger;
import io.swagger.models.Tag;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;

import java.io.File;
import java.io.FileFilter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class SwaggerController extends Controller {
    //设置需要扫描哪个包里面的类生产api
	public static final String BASE_PACKAGE="com.swaggerTest";
	public void index() {
		render("index.html");
	}

	public void api() {
		Swagger doc = new Swagger();
		Info info = new Info();
		Contact contact = new Contact();
		contact.setEmail("[email protected]");
		contact.setName("bimfoo");
		contact.setUrl("http://www.baidu.com");

		info.setDescription("项目接口的总体描述信息");
		License license = new License();
		license.setName("Apache 2.0");
		license.setUrl("http://www.baidu.com");
		info.setLicense(license);
		info.setTitle("运营平台接口");
		info.setTermsOfService("http://www.baidu.com");
		info.setVersion("2.0");
		info.setContact(contact);

		List schemes = new ArrayList<>();
		schemes.add(Scheme.HTTP);
		schemes.add(Scheme.HTTPS);

		Map paths = new HashMap<>();

		Set> classSet = getClassSet(BASE_PACKAGE);
		List tags = new ArrayList<>();
		for (Class cls : classSet) {
			if (cls.isAnnotationPresent(Api.class)) {
				Api api = cls.getAnnotation(Api.class);
				String[] apitags = api.tags();
				String apiValue = api.value();

				if (apitags.length > 0) {
					for (String tag : apitags) {
						Tag tagobj = new Tag();
						tagobj.setName(tag);
						tags.add(tagobj);
					}
				} else {
					Tag tagobj = new Tag();
					tagobj.setName(apiValue);
					tags.add(tagobj);
				}

				Method[] methods = cls.getMethods();

				for (Method method : methods) {
					Annotation[] annotations = method.getAnnotations();
					Path path = new Path();

					for (Annotation annotation : annotations) {
						// Class aclass = annotation.annotationType();
						Operation option = new Operation();
						String opvalue = "";
						if (method.isAnnotationPresent(ApiOperation.class)) {
							ApiOperation opertion = method.getAnnotation(ApiOperation.class);
							ArrayList produces = new ArrayList<>();
							String producesStr = opertion.produces();
							produces.add(producesStr);

							opvalue = opertion.value();
							String notes = opertion.notes();
							/*
							 * String consumesStr = opertion.consumes();
							 * String[] ptagsarray = opertion.tags();
							 */
							List ptags = new ArrayList<>();
							ptags.add(apitags[0]);
							/*
							 * if(ptagsarray.length>0){ for(String
							 * tag:ptagsarray){ ptags.add(tag); } }
							 */

							option.setConsumes(new ArrayList());
							option.setDescription(notes);
							option.setSummary(notes);
							option.setTags(ptags);

						}
						List parameters = new ArrayList<>();
						if (method.isAnnotationPresent(ApiImplicitParams.class)) {
							ApiImplicitParams apiImplicitParams = method.getAnnotation(ApiImplicitParams.class);
							ApiImplicitParam[] apiImplicitParamArray = apiImplicitParams.value();

							for (ApiImplicitParam param : apiImplicitParamArray) {
								PathParameter parameter = new PathParameter();
								String in = param.paramType();
								String name = param.name();
								String value = param.value();
								boolean required = param.required();
								String dataType = param.dataType();

								parameter.setType(dataType);
								parameter.setDefaultValue("");
								parameter.setDescription(value);
								parameter.setRequired(required);
								parameter.setIn(in);
								parameter.setName(name);
								parameters.add(parameter);
							}
						}
						option.setParameters(parameters);

						Map responseMap = new HashMap<>();
						if (method.isAnnotationPresent(ApiResponses.class)) {
							ApiResponses responses = method.getAnnotation(ApiResponses.class);
							ApiResponse[] responseArray = responses.value();
							for (ApiResponse response : responseArray) {
								String code = response.code() + "";
								String msg = response.message();
								Response res = new Response();
								res.setDescription(msg);
								responseMap.put(code, res);

							}

						}

						option.setResponses(responseMap);
						path.setGet(option);
						paths.put(opvalue, path);
					}

				}
				doc.setSchemes(schemes);
				doc.setSwagger("2.0");
				doc.setBasePath("");
				doc.setInfo(info);
				doc.setHost("http://localhost");
				doc.setTags(tags);
				doc.setPaths(paths);

			}

		}
		Gson gson = new Gson();
		String json = gson.toJson(doc);
		renderText(json);
	}

	/**
	 * 获取指定包名下所有类
	 *
	 * @param packageName
	 * @return
	 */
	public static Set> getClassSet(String packageName) {
		Set> classSet = new HashSet>();
		try {
			Enumeration urls = getClassLoader().getResources(packageName.replace(".", "/"));
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				if (url != null) {
					String protocol = url.getProtocol();
					if (protocol.equals("file")) {
						String packagePath = url.getPath().replace("%20", " ");
						addClass(classSet, packagePath, packageName);
					} else if (protocol.equals("jar")) {
						JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
						if (jarURLConnection != null) {
							JarFile jarFile = jarURLConnection.getJarFile();
							if (jarFile != null) {
								Enumeration jarEntries = jarFile.entries();
								while (jarEntries.hasMoreElements()) {
									JarEntry jarEntry = jarEntries.nextElement();
									String jarEntryName = jarEntry.getName();
									if (jarEntryName.endsWith(".class")) {
										String className = jarEntryName.substring(0, jarEntryName.lastIndexOf("."))
												.replaceAll("/", ".");
										doAddClass(classSet, className);
									}
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return classSet;
	}

	/**
	 * 获取类加载器
	 *
	 * @return
	 */
	public static ClassLoader getClassLoader() {
		return Thread.currentThread().getContextClassLoader();
	}

	private static void addClass(Set> classSet, String packagePath, String packageName) {
		File[] files = new File(packagePath).listFiles(new FileFilter() {
			public boolean accept(File file) {
				return (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory();
			}
		});
		for (File file : files) {
			String fileName = file.getName();
			if (file.isFile()) {
				String className = fileName.substring(0, fileName.lastIndexOf("."));
				if (StringUtil.isNotEmpty(packageName)) {
					className = packageName + "." + className;
				}
				doAddClass(classSet, className);
			} else {
				String subPackagePath = fileName;
				if (StringUtil.isNotEmpty(packagePath)) {
					subPackagePath = packagePath + "/" + subPackagePath;
				}
				String subPackageName = fileName;
				if (StringUtil.isNotEmpty(packageName)) {
					subPackageName = packageName + "." + subPackageName;
				}
				addClass(classSet, subPackagePath, subPackageName);
			}
		}
	}

	private static void doAddClass(Set> classSet, String className) {
		Class cls = loadClass(className, false);
		classSet.add(cls);
	}

	/**
	 * 加载类
	 *
	 * @param className
	 * @param isInitialized
	 *            false 代表装载类的时候 不进行初始化工作[不会执行静态代码块]
	 * @return
	 */
	public static Class loadClass(String className, boolean isInitialized) {
		Class cls;
		try {
			cls = Class.forName(className, isInitialized, getClassLoader());
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
		return cls;
	}
}

pom.xml引入
        io.springfox
        springfox-swagger2
        2.2.2
    

    
        io.springfox
        springfox-swagger-ui
        2.2.2
    


    com.google.code.gson
    gson
    2.8.5

下载你需要的swagger-ui官方下载地址解压后把里面的dist内的文件拷贝到webapp里,然后打开index.html修改里面的url到能访问到我们新建controller的api函数,启动项目跳转到index.html 就可以了.这里就不很死板的说哪儿怎么配,原理就是这样,自己配routes就好了

最后别忘了在需要显示的方法和controller上面写注解像这样

 

@Api(tags="项目相关接口")
public class ProjectAction {
	@ApiOperation(value = "/getProjectById", httpMethod = "GET", 
		    consumes="application/json;charset=UFT-8",produces="application/json;charset=UFT-8",
		    notes = "根据项目id获取项目,更详细的说明")
	@ApiImplicitParams(@ApiImplicitParam(required = true, name = "pid", value = "项目的id",dataType="Long"))
	@ApiResponses({ @ApiResponse(code = 200, message = "Success"),@ApiResponse(code = 400, message = "Invalid Order") })
	public void getProjectById(){
		
	}
	
	@ApiOperation(value = "/getProjectByName", httpMethod = "GET", 
		    consumes="application/json;charset=UFT-8",produces="application/json;charset=UFT-8",
		    notes = "根据项目名称获取项目,更详细的说明")
	@ApiImplicitParams(@ApiImplicitParam(required = true, name = "pname", value = "项目的名称",dataType="String"))
	@ApiResponses({ @ApiResponse(code = 200, message = "Success"),@ApiResponse(code = 400, message = "Invalid Order") })
	public void getProjectByName(){
		
	}
}

 

你可能感兴趣的:(jfinal)