From c0eb73ec63371184da61e2c406598c37ffc84e54 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Thu, 28 Dec 2017 11:23:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20swagger=E5=8F=AF=E8=83=BD=E4=B9=B1?= =?UTF-8?q?=E7=A0=81,undertow=E6=97=A0=E6=B3=95=E4=B8=8Eswagger=E4=B8=80?= =?UTF-8?q?=E8=B5=B7=E5=B7=A5=E4=BD=9C,druid=20stat=20view=E6=B2=A1?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E7=94=A8=E6=88=B7=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 过程是这样的: 改simple-dao, 加上swagger注解和链接,发现没提示druid用户名 改simple-sharding-jdbc, 加上swagger注解和链接,发现NutFilterStarter有点挫,改造一下 改simple-undertow, 加上swagger注解和链接, 发现404和乱码 --- .../nutzboot-demo-simple-dao/pom.xml | 5 ++ .../nutz/demo/simple/module/UserModule.java | 15 ++++- .../src/main/resources/static/index.html | 12 +++- .../src/main/resources/static/index.html | 14 ++++- .../nutzboot-demo-simple-undertow/pom.xml | 2 +- .../io/nutz/demo/simple/MainLauncher.java | 5 ++ .../src/main/resources/static/index.html | 9 ++- .../jdbc/DruidWebStatServletStarter.java | 2 +- .../nutz/boot/starter/jetty/JettyStarter.java | 15 +---- .../starter/nutz/mvc/NutFilterStarter.java | 11 +++- .../swagger/SwaggerServletStarter.java | 1 + .../undertow/ComboResourceManager.java | 63 +++++++++++++++++++ .../starter/undertow/UndertowStarter.java | 28 ++++++--- 13 files changed, 150 insertions(+), 32 deletions(-) create mode 100644 nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/ComboResourceManager.java diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/pom.xml b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/pom.xml index 0bd1d136..8b2057b9 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/pom.xml +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/pom.xml @@ -28,6 +28,11 @@ nutzboot-starter-jetty ${project.version} + + org.nutz + nutzboot-starter-swagger + ${project.version} + org.slf4j slf4j-log4j12 diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/java/io/nutz/demo/simple/module/UserModule.java b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/java/io/nutz/demo/simple/module/UserModule.java index d4c66073..49f99177 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/java/io/nutz/demo/simple/module/UserModule.java +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/java/io/nutz/demo/simple/module/UserModule.java @@ -12,7 +12,12 @@ import org.nutz.mvc.annotation.Ok; import org.nutz.mvc.annotation.Param; import io.nutz.demo.simple.bean.User; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +@Api("user") @At("/user") @IocBean public class UserModule { @@ -20,14 +25,20 @@ public class UserModule { @Inject Dao dao; + @ApiOperation(value = "获取用户总数", notes = "获取用户总数", httpMethod="GET", response=Long.class) @Ok("raw") - @At("/count") + @At public long count() { return dao.count(User.class); } + @ApiOperation(value = "查询用户列表", notes = "可分页", httpMethod="GET") + @ApiImplicitParams({ + @ApiImplicitParam(name = "pageNumber", paramType="query", value = "起始页是1", dataType="int", required = false, defaultValue="1"), + @ApiImplicitParam(name = "pageSize", paramType="query", value = "每页数量", dataType="int", required = false, defaultValue="20"), + }) @Ok("json:full") - @At("/query") + @At public List query(@Param("..")Pager pager) { return dao.query(User.class, Cnd.orderBy().asc("age"), pager); } diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/resources/static/index.html b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/resources/static/index.html index 279b8e13..61f6ae4a 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/resources/static/index.html +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-dao/src/main/resources/static/index.html @@ -5,6 +5,16 @@ Hello, So NB! -Hello, So NB! +
+

Hello, So NB!

+
+
+

+ 查看api文档(由Swagger生成) +

+
+
+

Druid监控页面,用户名密码在日志搜索 "druid stat view"

+
\ No newline at end of file diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-sharding-jdbc/src/main/resources/static/index.html b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-sharding-jdbc/src/main/resources/static/index.html index 279b8e13..a43c0a10 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-sharding-jdbc/src/main/resources/static/index.html +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-sharding-jdbc/src/main/resources/static/index.html @@ -5,6 +5,18 @@ Hello, So NB! -Hello, So NB! +
+

Hello, So NB!

+
+
+

+ 访问用户总数 +

+
+
+

+ 查询用户列表 +

+
\ No newline at end of file diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/pom.xml b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/pom.xml index db35707d..67665444 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/pom.xml +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/pom.xml @@ -20,7 +20,7 @@
org.nutz - nutzboot-starter-shiro + nutzboot-starter-swagger ${project.version} diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/java/io/nutz/demo/simple/MainLauncher.java b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/java/io/nutz/demo/simple/MainLauncher.java index 855db089..be3e0e36 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/java/io/nutz/demo/simple/MainLauncher.java +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/java/io/nutz/demo/simple/MainLauncher.java @@ -5,9 +5,14 @@ import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.mvc.annotation.At; import org.nutz.mvc.annotation.Ok; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; + +@Api("demo") @IocBean public class MainLauncher { + @ApiOperation(value = "获取当前毫秒数", notes="获取当前毫秒数", httpMethod="GET", response=Long.class) @Ok("raw") @At("/time/now") public long now() { diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/resources/static/index.html b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/resources/static/index.html index 279b8e13..f5e32e05 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/resources/static/index.html +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-undertow/src/main/resources/static/index.html @@ -5,6 +5,13 @@ Hello, So NB! -Hello, So NB! +
+

Hello, So NB!

+
+
+

+ 查看api文档(由Swagger生成) +

+
\ No newline at end of file diff --git a/nutzboot-starter-jdbc/src/main/java/org/nutz/boot/starter/jdbc/DruidWebStatServletStarter.java b/nutzboot-starter-jdbc/src/main/java/org/nutz/boot/starter/jdbc/DruidWebStatServletStarter.java index 45e28cb6..2ccd2002 100644 --- a/nutzboot-starter-jdbc/src/main/java/org/nutz/boot/starter/jdbc/DruidWebStatServletStarter.java +++ b/nutzboot-starter-jdbc/src/main/java/org/nutz/boot/starter/jdbc/DruidWebStatServletStarter.java @@ -70,7 +70,7 @@ public class DruidWebStatServletStarter implements WebServletFace { if (!_tmp.containsKey(StatViewServlet.PARAM_NAME_PASSWORD)) { String pwd = R.UU32(); _tmp.put(StatViewServlet.PARAM_NAME_PASSWORD, pwd); - log.infof("druid stat view random password=%s", pwd); + log.infof("druid stat view random user=%s password=%s", _tmp.get(StatViewServlet.PARAM_NAME_USERNAME), pwd); } for (Entry en : _tmp.entrySet()) { params.put(en.getKey(), String.valueOf(en.getValue())); diff --git a/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java b/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java index a19393ca..52907a46 100644 --- a/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java +++ b/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java @@ -134,7 +134,7 @@ public class JettyStarter implements ClassLoaderAware, IocAware, ServerFace, Lif // wac.setCopyWebInf(true); // wac.setProtectedTargets(new String[]{"/java", "/javax", "/org", // "/net", "/WEB-INF", "/META-INF"}); - wac.setTempDirectory(createTempDir("jetty").getAbsoluteFile()); + wac.setTempDirectory(new File("temp")); wac.setClassLoader(classLoader); wac.setConfigurationDiscovered(true); if (System.getProperty("os.name").toLowerCase().contains("windows")) { @@ -222,19 +222,6 @@ public class JettyStarter implements ClassLoaderAware, IocAware, ServerFace, Lif public void depose() throws Exception {} - private File createTempDir(String prefix) { - try { - File tempDir = File.createTempFile(prefix + ".", "." + getPort()); - tempDir.delete(); - tempDir.mkdir(); - tempDir.deleteOnExit(); - return tempDir; - } - catch (IOException ex) { - throw new RuntimeException("Unable to create tempDir. java.io.tmpdir is set to " + System.getProperty("java.io.tmpdir"), ex); - } - } - // --getConf--- public int getPort() { return conf.getInt(PROP_PORT, 8080); diff --git a/nutzboot-starter-nutz-mvc/src/main/java/org/nutz/boot/starter/nutz/mvc/NutFilterStarter.java b/nutzboot-starter-nutz-mvc/src/main/java/org/nutz/boot/starter/nutz/mvc/NutFilterStarter.java index 02e8d55f..ece7dc52 100644 --- a/nutzboot-starter-nutz-mvc/src/main/java/org/nutz/boot/starter/nutz/mvc/NutFilterStarter.java +++ b/nutzboot-starter-nutz-mvc/src/main/java/org/nutz/boot/starter/nutz/mvc/NutFilterStarter.java @@ -7,7 +7,9 @@ import java.util.Map; import javax.servlet.DispatcherType; import javax.servlet.Filter; +import org.nutz.boot.AppContext; import org.nutz.boot.starter.WebFilterFace; +import org.nutz.boot.starter.WebServletFace; import org.nutz.ioc.Ioc; import org.nutz.ioc.impl.PropertiesProxy; import org.nutz.ioc.loader.annotation.Inject; @@ -23,6 +25,9 @@ public class NutFilterStarter implements WebFilterFace { @Inject protected PropertiesProxy conf; + + @Inject + protected AppContext appContext; public String getName() { return "nutz"; @@ -51,7 +56,11 @@ public class NutFilterStarter implements WebFilterFace { if (conf.has("nutz.mvc.ignore")) { params.put("ignore", conf.get("nutz.mvc.ignore")); } - params.put("exclusions", conf.get("nutz.mvc.exclusions", "/druid/*,/uflo/*,/ureport/*,/urule/*,/webservice/*,/swagger/*")); + StringBuilder sb = new StringBuilder(); + for (WebServletFace face : appContext.getBeans(WebServletFace.class)) { + sb.append(',').append(face.getPathSpec()); + } + params.put("exclusions", conf.get("nutz.mvc.exclusions", "") + sb); return params; } diff --git a/nutzboot-starter-swagger/src/main/java/org/nutz/boot/starter/swagger/SwaggerServletStarter.java b/nutzboot-starter-swagger/src/main/java/org/nutz/boot/starter/swagger/SwaggerServletStarter.java index 02c78da5..2306a6d9 100644 --- a/nutzboot-starter-swagger/src/main/java/org/nutz/boot/starter/swagger/SwaggerServletStarter.java +++ b/nutzboot-starter-swagger/src/main/java/org/nutz/boot/starter/swagger/SwaggerServletStarter.java @@ -72,6 +72,7 @@ public class SwaggerServletStarter extends HttpServlet implements WebServletFace final String pathInfo = request.getRequestURI(); if (pathInfo.endsWith("/swagger.json")) { response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); response.getWriter().println(Json.mapper().writeValueAsString(swagger)); } else { response.setStatus(404); diff --git a/nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/ComboResourceManager.java b/nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/ComboResourceManager.java new file mode 100644 index 00000000..a5cc23fd --- /dev/null +++ b/nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/ComboResourceManager.java @@ -0,0 +1,63 @@ +package org.nutz.boot.starter.undertow; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.nutz.lang.Streams; + +import io.undertow.server.handlers.resource.Resource; +import io.undertow.server.handlers.resource.ResourceChangeListener; +import io.undertow.server.handlers.resource.ResourceManager; + +public class ComboResourceManager implements ResourceManager { + + protected List managers = new ArrayList<>(); + + public void close() throws IOException { + managers.forEach((action)->Streams.safeClose(action)); + } + + public Resource getResource(String path) throws IOException { + for (ResourceManager manager : managers) { + Resource resource = manager.getResource(path); + if (resource != null) + return resource; + } + return null; + } + + public boolean isResourceChangeListenerSupported() { + for (ResourceManager manager : managers) { + if (manager.isResourceChangeListenerSupported()) + return true; + } + return false; + } + + public void registerResourceChangeListener(ResourceChangeListener listener) { + for (ResourceManager manager : managers) { + if (manager.isResourceChangeListenerSupported()) + manager.registerResourceChangeListener(listener); + } + } + + public void removeResourceChangeListener(ResourceChangeListener listener) { + for (ResourceManager manager : managers) { + if (manager.isResourceChangeListenerSupported()) + manager.registerResourceChangeListener(listener); + } + } + + public void add(ResourceManager manager) { + managers.add(manager); + } + + public void remove(ResourceManager manager) { + managers.remove(manager); + } + + public void clear() { + managers.clear(); + } +} diff --git a/nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/UndertowStarter.java b/nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/UndertowStarter.java index 14ecceb9..919545d4 100644 --- a/nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/UndertowStarter.java +++ b/nutzboot-starter-undertow/src/main/java/org/nutz/boot/starter/undertow/UndertowStarter.java @@ -1,6 +1,7 @@ package org.nutz.boot.starter.undertow; import java.io.File; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.EventListener; @@ -25,7 +26,7 @@ import org.nutz.ioc.Ioc; import org.nutz.ioc.impl.PropertiesProxy; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; -import org.nutz.lang.Files; +import org.nutz.lang.Strings; import org.nutz.lang.util.LifeCycle; import org.nutz.log.Log; import org.nutz.log.Logs; @@ -65,10 +66,10 @@ public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace, @PropDoc(group = "undertow", value = "上下文路径", defaultValue = "/") public static final String PROP_CONTEXT_PATH = PRE + "contextPath"; - @PropDoc(group = "undertow", value = "session过期时间", defaultValue = "30") + @PropDoc(group = "undertow", value = "session过期时间,单位分钟", defaultValue = "30", type="int") public static final String PROP_SESSION = PRE + "session"; - @PropDoc(group = "undertow", value = "静态文件路径", defaultValue = "/static/") + @PropDoc(group = "undertow", value = "静态文件路径", defaultValue = "static/") public static final String PROP_STATIC_PATH = PRE + "staticPath"; @Inject @@ -117,12 +118,13 @@ public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace, deployment = Servlets.deployment().setDeploymentName("nb").setClassLoader(classLoader).setEagerFilterInit(true).setSecurityDisabled(true); deployment.setContextPath(contextPath).setDefaultSessionTimeout(getSessionTimeout()); - File resRootDir = Files.findFile(getStaticPath()); - if (resRootDir != null && resRootDir.isDirectory()) { - deployment.setResourceManager(new FileResourceManager(resRootDir, 1024)); - } else { - deployment.setResourceManager(new ClassPathResourceManager(classLoader, getStaticPath())); - } + ComboResourceManager resourceManager = new ComboResourceManager(); + for (String path : getResourcePaths()) { + if (new File(path).exists()) + resourceManager.add(new FileResourceManager(new File(path), 1024)); + resourceManager.add(new ClassPathResourceManager(classLoader, path)); + } + deployment.setResourceManager(resourceManager); addNutzSupport(); addWebSocketSupport(); @@ -220,7 +222,6 @@ public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace, public void depose() throws Exception { } - // --getConf--- public int getPort() { return conf.getInt(PROP_PORT, 8080); @@ -233,6 +234,13 @@ public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace, public String getStaticPath() { return conf.get(PROP_STATIC_PATH, "static"); } + + public List getResourcePaths() { + if (Strings.isBlank(conf.get(PROP_STATIC_PATH)) && + ("static".equals(conf.get(PROP_STATIC_PATH)) || "static/".equals(conf.get(PROP_STATIC_PATH)))) + return Arrays.asList("static", "webapp"); + return Arrays.asList("static", "webapp"); + } public String getContextPath() { return conf.get(PROP_CONTEXT_PATH, "/");