今日遇到一个十分棘手的问题:在eclipse中编译没问题,但是gradle打包却通不过,经过同事的提醒才知道可能是环境的不同导致的。然后去检查才发现:eclipse使用的是JRE的lib,但是gradle的设置却用的是JDK-HOME的,但是JDK-HOME没有rt.jar,导致打包失败。
将JRE的lib/rt.jar拷贝过去即可。
另外一种gradle失败的情况是找不到中央库
Cannot resolve external dependency mysql:mysql-connector-java:5.1.38 because no
repositories are defined.
Required by:
:dzg-server-api:3.2.1
加上或者其他的即可
repositories {
jcenter()
}
logback打印两次的问题是由于:
Jersey可以通过注册ContainerRequestFilter和ContainerResponseFilter或者VertxRequestProcessor和VertxResponseProcessor来统一获取入参和出参。
public class RequestLogFilter implements ContainerRequestFilter{
private static final SimpleDateFormat SDFLONG = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Logger logger = LoggerFactory.getLogger(RequestLogFilter.class);
@Override
public void filter(ContainerRequestContext requestContext) throws IOException{
try{
reportLog(requestContext);
}
catch(Exception e){
e.printStackTrace();
logger.error("获取日志信息失败" + e.getMessage());
}
}
/**
* @param requestContext
* @throws IOException
*/
public void reportLog(ContainerRequestContext requestContext) throws IOException{
// 把所有请求的数据保存下来
InputStream stream = requestContext.getEntityStream();
byte[] bs = new byte[stream.available()];
stream.read(bs);
// 重新设置EntityStream
ByteArrayInputStream bai = new ByteArrayInputStream(bs);
requestContext.setEntityStream(bai);// 为了后面的处理,因为流只能被读一次。
MultivaluedMap headers = requestContext.getHeaders();
MediaType type = requestContext.getMediaType();
report(requestContext.getUriInfo().getRequestUri().toString(),headers, new String(bs), requestContext.getMethod(),
type == null ? "unknown mediaType!" : type.toString(),
requestContext.getUriInfo().getMatchedResources());
}
private void report(String url,MultivaluedMap headers, String entity, String method, String mediaType, List
public class ResponseLogFilter implements ContainerResponseFilter{
private static final SimpleDateFormat SDFLONG = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Logger logger = LoggerFactory.getLogger(ResponseLogFilter.class);
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException{
try{
reportLog(responseContext);
}
catch(Exception e){
e.printStackTrace();
logger.error("获取日志信息失败" + e.getMessage());
}
}
public void reportLog(ContainerResponseContext responseContext){
Object entity = responseContext.getEntity();
MultivaluedMap headers = responseContext.getStringHeaders();
MediaType type = responseContext.getMediaType();
report(headers, entity, type == null ? "unknown mediaType!" : type.toString());
}
private void report(MultivaluedMap headers, Object entity, String mediaType){
StringBuilder sb = new StringBuilder("\nJersey response report start-------- ")
.append(SDFLONG.format(new Date())).append(" ------------------------------");
sb.append("\nMediaType : ").append(mediaType);
sb.append("\nHeaders : ");
for(Entry> entry:headers.entrySet()){
List strs = entry.getValue();
sb.append(entry.getKey()).append("->").append(strs.size()>1?strs:strs.get(0)).append("\r\n");
}
sb.append("Entity/Params : ").append(entity);
sb.append("\nJersey response report end-------------------------------------------------------------\n\n");
logger.info(sb.toString());
}
}
public class FilterBinder extends AbstractBinder{
/**
* Implement to provide binding definitions using the exposed binding
* methods.
*/
@Override
protected void configure(){
bind(RequestLogFilter.class).to(ContainerRequestFilter.class);
bind(ResponseLogFilter.class).to(ContainerResponseFilter.class);
}
}
public class RequestProcessor implements VertxRequestProcessor {
private static final SimpleDateFormat SDFLONG = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Logger logger = LoggerFactory.getLogger(RequestProcessor.class);
@Override
public void process(HttpServerRequest vertxRequest,
ContainerRequest jerseyRequest, Handler done) {
try{
reportLog(jerseyRequest);
}
catch(Exception e){
e.printStackTrace();
logger.error("获取日志信息失败" + e.getMessage());
}
long now = System.currentTimeMillis();
done.handle(null);
long now2 = System.currentTimeMillis();
logger.info(jerseyRequest.getUriInfo().getRequestUri().toString()+" 接口一共执行了 "+(now2-now) + "ms 时间\n");
}
/**
* @param requestContext
* @throws IOException
*/
public void reportLog(ContainerRequestContext requestContext) throws IOException{
// 把所有请求的数据保存下来
InputStream stream = requestContext.getEntityStream();
byte[] bs = new byte[stream.available()];
stream.read(bs);
// 重新设置EntityStream
ByteArrayInputStream bai = new ByteArrayInputStream(bs);
requestContext.setEntityStream(bai);// 为了后面的处理,因为流只能被读一次。
MultivaluedMap headers = requestContext.getHeaders();
MediaType type = requestContext.getMediaType();
report(requestContext.getUriInfo().getRequestUri().toString(),headers, new String(bs), requestContext.getMethod(),
type == null ? "unknown mediaType!" : type.toString(),
requestContext.getUriInfo().getMatchedResources());
}
private void report(String url,MultivaluedMap headers, String entity, String method, String mediaType, List
public class ResponseProcessor implements VertxResponseProcessor{
private static final SimpleDateFormat SDFLONG = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Logger logger = LoggerFactory.getLogger(ResponseLogFilter.class);
@Override
public void process(HttpServerResponse vertxResponse, ContainerResponse jerseyResponse){
try{
reportLog(jerseyResponse);
}
catch(Exception e){
e.printStackTrace();
logger.error("获取日志信息失败" + e.getMessage());
}
}
public void reportLog(ContainerResponseContext responseContext){
Object entity = responseContext.getEntity();
MultivaluedMap headers = responseContext.getStringHeaders();
MediaType type = responseContext.getMediaType();
report(headers, entity, type == null ? "unknown mediaType!" : type.toString());
}
private void report(MultivaluedMap headers, Object entity, String mediaType){
StringBuilder sb = new StringBuilder("\nJersey response report start-------- ")
.append(SDFLONG.format(new Date())).append(" ------------------------------");
sb.append("\nMediaType : ").append(mediaType);
sb.append("\nHeaders : ");
for(Entry> entry:headers.entrySet()){
List strs = entry.getValue();
sb.append(entry.getKey()).append("->").append(strs.size()>1?strs:strs.get(0)).append("\r\n");
}
sb.append("Entity/Params : ").append(entity);
sb.append("\nJersey response report end-------------------------------------------------------------\n");
logger.info(sb.toString());
}
}
/**
* HK2 binder for injecting filters
*/
public class FilterBinder extends AbstractBinder{
/**
* Implement to provide binding definitions using the exposed binding
* methods.
*/
@Override
protected void configure(){
bind(RequestProcessor.class).to(VertxRequestProcessor.class);
bind(ResponseProcessor.class).to(VertxResponseProcessor.class);
}
}
使用Professor还可以统计接口返回时间
从gradle 2.12开始,可以使用compileOnly来引入类似servlet-api的jar包,可以达到只编译不引入的目的,类似maven的provided。
ngrok:让外网访问内网工程
https://www.ngrok.cc
frp内网穿透