mirror of
https://gitee.com/nutz/nutzboot.git
synced 2024-12-03 12:18:50 +08:00
fix: swagger可能乱码,undertow无法与swagger一起工作,druid stat view没打印用户名
过程是这样的: 改simple-dao, 加上swagger注解和链接,发现没提示druid用户名 改simple-sharding-jdbc, 加上swagger注解和链接,发现NutFilterStarter有点挫,改造一下 改simple-undertow, 加上swagger注解和链接, 发现404和乱码
This commit is contained in:
parent
54f2446ada
commit
c0eb73ec63
@ -28,6 +28,11 @@
|
||||
<artifactId>nutzboot-starter-jetty</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-swagger</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
|
@ -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<User> query(@Param("..")Pager pager) {
|
||||
return dao.query(User.class, Cnd.orderBy().asc("age"), pager);
|
||||
}
|
||||
|
@ -5,6 +5,16 @@
|
||||
<title>Hello, So NB!</title>
|
||||
</head>
|
||||
<body>
|
||||
Hello, So NB!
|
||||
<div>
|
||||
<h2>Hello, So NB!</h2>
|
||||
</div>
|
||||
<div>
|
||||
<h2>
|
||||
<a href="swagger/index.html">查看api文档(由Swagger生成)</a>
|
||||
</h2>
|
||||
</div>
|
||||
<div>
|
||||
<h2><a href="druid/">Druid监控页面,用户名密码在日志搜索 "druid stat view"</a></h2>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -5,6 +5,18 @@
|
||||
<title>Hello, So NB!</title>
|
||||
</head>
|
||||
<body>
|
||||
Hello, So NB!
|
||||
<div>
|
||||
<h2>Hello, So NB!</h2>
|
||||
</div>
|
||||
<div>
|
||||
<h2>
|
||||
<a href="user/count">访问用户总数</a>
|
||||
</h2>
|
||||
</div>
|
||||
<div>
|
||||
<h2>
|
||||
<a href="user/query">查询用户列表</a>
|
||||
</h2>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -20,7 +20,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-shiro</artifactId>
|
||||
<artifactId>nutzboot-starter-swagger</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -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() {
|
||||
|
@ -5,6 +5,13 @@
|
||||
<title>Hello, So NB!</title>
|
||||
</head>
|
||||
<body>
|
||||
Hello, So NB!
|
||||
<div>
|
||||
<h2>Hello, So NB!</h2>
|
||||
</div>
|
||||
<div>
|
||||
<h2>
|
||||
<a href="swagger/index.html">查看api文档(由Swagger生成)</a>
|
||||
</h2>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -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<String, Object> en : _tmp.entrySet()) {
|
||||
params.put(en.getKey(), String.valueOf(en.getValue()));
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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<ResourceManager> 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();
|
||||
}
|
||||
}
|
@ -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<String> 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, "/");
|
||||
|
Loading…
Reference in New Issue
Block a user