1. How to obtain the request and response in spring aspect programming
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();//Get request HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();//Get response
2. Example of httpClient
String url = "http://localhost:8080/nap/api/inquiry/v2"; String method = "POST"; String[] datas = new String[10]; datas[0]="{\"ver\":400,\"sid\":101,\"voyType\":\"ST\",\"passengers\":{\"adtCount\":1,\"chdCount\":0},\"voys\":[{\"orgCity\":\"BJS\",\"dstCity\":\"SHA\",\"deptDate\":\"2017-03-30\"}]}"; datas[1]="{\"ver\":400,\"sid\":101,\"voyType\":\"ST\",\"passengers\":{\"adtCount\":1,\"chdCount\":0},\"voys\":[{\"orgCity\":\"BJS\",\"dstCity\":\"SHA\",\"deptDate\":\"2017-03-21\"}]}"; DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter("http.protocol.content-charset", "utf-8"); httpClient.getParams().setParameter("http.socket.timeout", 15 * 1000); httpClient.getParams().setParameter("http.connection.timeout", 15 * 1000); BasicResponseHandler e = new BasicResponseHandler(); HttpPost req1 = new HttpPost(url); req1.setEntity(new StringEntity(datas[index])); String ret = httpclient.execute(req1, e);
3. The getReader() and getInputStream() of the request in the http request can only be read once. The following is the implementation scheme of springboot
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * http Implementation of repeated reading of request input parameter reader stream * Created by ysma on 2018/5/22. */ @WebFilter(urlPatterns = "/*") public class HttpParamFilter implements Filter{ private static final Logger LOGGER = LoggerFactory.getLogger(HttpParamFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { LOGGER.debug("======HttpParamFilter init=========="); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse resp = (HttpServletResponse)response; TnfHttpServletRequestWrapper tnfHttpServletRequestWrapper = new TnfHttpServletRequestWrapper(req); filterChain.doFilter(tnfHttpServletRequestWrapper, resp); } @Override public void destroy() { LOGGER.debug("======HttpParamFilter destroyed=========="); } } ====================Dividing line======================= import org.apache.commons.codec.binary.Base64; import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; /** * tnf Custom httpServletRequest * Created by ysma on 2018/5/22. */ public class TnfHttpServletRequestWrapper extends HttpServletRequestWrapper{ private byte[] bytes; /** * Constructs a request object wrapping the given request. * * @param request * @throws IllegalArgumentException if the request is null */ public TnfHttpServletRequestWrapper(HttpServletRequest request) throws IOException { super(request); StringBuilder buffer = new StringBuilder(); BufferedReader reader = request.getReader(); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } String paramStr = buffer.toString(); byte[] originBytes = paramStr.getBytes(); if (Base64.isBase64(originBytes)) { bytes = Base64.decodeBase64(originBytes); } else { bytes = originBytes; } } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream baInputStream = new ByteArrayInputStream(bytes); return new ServletInputStream() { @Override public boolean isFinished() { return false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener readListener) { } public int read() throws IOException { return baInputStream.read(); } }; } /** * The default behavior of this method is to return getReader() * on the wrapped request object. */ @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(this.getInputStream())); } }
4. ajax cross domain request setting, another server implementation scheme other than jsonp
response.setHeader("Access-Control-Allow-Origin", "*");
5. Self implementation configuration file reading
Configuration config = new PropertiesConfiguration("yourfile.properties"); If you need to use automatic loading, you only need to declare an automatic overload policy in the annual id configuration information: config.setReloadingStrategy(new FileChangedReloadingStrategy()); Special characters If you need to use some special characters, such as line breaks, tabs, or unicode characters, you need to escape the characters. The string separator (comma "," by default) also needs to be escaped: key = This /n string /t contains /, escaped // characters /u0020 colors.pie = #FF0000, #00FF00, #0000FF Instead of manually splitting strings, you can directly return them as an array: String[] colors = config.getStringArray("colors.pie"); getList(key), etc....
6. spring extension agent class
public class MyInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { MyMethod myMethod = method.getAnnotation(MyMethod.class); String serviceName = myMethod.serviceName(); //TODO serviceName to open source China say hello Object result = callOschina(serviceName); return result; } } //Agent class is finished, so how to register? public class MyProxyAutowireCandidateResolver extends QualifierAnnotationAutowireCandidateResolver { @Override public Object getSuggestedValue(DependencyDescriptor descriptor) { Class<?> clazz = descriptor.getDependencyType(); if (clazz.isInterface() && MyInterface.class.isAssignableFrom(clazz)) { return createProxy(clazz); } return super.getSuggestedValue(descriptor); } private Object createProxy(Class<?> clazz) { return Proxy.newProxyInstance(MyProxyAutowireCandidateResolver.class.getClassLoader(), new Class[] { clazz }, new MyInvocationHandler()); } }