Home > Back-end >  Spring how to use the HTTP requests with aop print clear intertextuality?
Spring how to use the HTTP requests with aop print clear intertextuality?

Time:03-10

I hope that in the spring to write a debugging using aop, can print HTTP requests and responses of plaintext

Which is able to print such a request clear
 
POST/echo HTTP/1.1
Host: localhost
The content-type: text/plain. Charset=utf-8

{" ABC ":" xyz "}

Print the request and response
 
HTTP/1.1 200
The content-type: text/plain. Charset=utf-8
Date: Mon, 08 Mar 2021 09:57:23 GMT

{" ABC ":" xyz "}

CodePudding user response:

Can you make an AOP sweep controller under the package all interfaces, get into arguments, print it out, the return of the interface don't know if you can get

CodePudding user response:

reference 1st floor KeepSayingNo response:
can you make an AOP sweep controller under the package all interfaces, get into arguments, print it out, the return of the interface don't know if you can get

Small white can't write need code demonstrate how to intercept the spring request channel

CodePudding user response:

Like upstairs brothers said, make a AOP, only the following test reference, basic all needed information in the request, the original poster to this do? Access log statistics? Still have other use, there is some information on the access log, or nginx log on
 package org. Outerwaterfire. Springweb_test. Web; 

The import com. Google. Gson. Gson;
The import com. Google. Gson. GsonBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
The import org. Springframework. Web. Bind. The annotation. RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@ RestController
@ RequestMapping ("/Hello! ")
Public class TestController {

@ RequestMapping ("/haha ")
Public String haha (it it) {
Map Headers=new HashMap<> (a);
Enumeration headerNames=it. GetHeaderNames ();

While (headerNames. HasMoreElements ()) {
String headerName=(String) headerNames. NextElement ();
Headers. The put (headerName, it getHeader (headerName));

}
Headers. The put (" method ", it getMethod ());
Headers. The put (" protocol ", it getProtocol ());
Gson Gson=new GsonBuilder (). The create ();
System. The out. Println (" headers===="+ gson. ToJson (headers));
Return "done";

}
}

CodePudding user response:

reference hbhbhbhbhb1021 reply: 3/f
like upstairs brothers said, make a AOP, only the following test reference, basic all needed information in the request, the original poster to this do? Access log statistics? Still have other use, there is some information on the access log, or nginx log on
 package org. Outerwaterfire. Springweb_test. Web; 

The import com. Google. Gson. Gson;
The import com. Google. Gson. GsonBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
The import org. Springframework. Web. Bind. The annotation. RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@ RestController
@ RequestMapping ("/Hello! ")
Public class TestController {

@ RequestMapping ("/haha ")
Public String haha (it it) {
Map Headers=new HashMap<> (a);
Enumeration headerNames=it. GetHeaderNames ();

While (headerNames. HasMoreElements ()) {
String headerName=(String) headerNames. NextElement ();
Headers. The put (headerName, it getHeader (headerName));

}
Headers. The put (" method ", it getMethod ());
Headers. The put (" protocol ", it getProtocol ());
Gson Gson=new GsonBuilder (). The create ();
System. The out. Println (" headers===="+ gson. ToJson (headers));
Return "done";

}
}

Is in order to convenient debugging, this I can't write
1. Don't know how to get the input output stream
2. This stream can only read and write a blocked certainly affects normal spring rest controller, how to solve the problem is not repeatable read
3. The controller and I write, the aspect I would have written, I am not understand spring, don't know how to intercept, intercept to also won't solve the problem of stream not repeatable read and write

CodePudding user response:

reference hbhbhbhbhb1021 reply: 3/f
like upstairs brothers said, make a AOP, only the following test reference, basic all needed information in the request, the original poster to this do? Access log statistics? Still have other use, there is some information on the access log, or nginx log on
 package org. Outerwaterfire. Springweb_test. Web; 

The import com. Google. Gson. Gson;
The import com. Google. Gson. GsonBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
The import org. Springframework. Web. Bind. The annotation. RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@ RestController
@ RequestMapping ("/Hello! ")
Public class TestController {

@ RequestMapping ("/haha ")
Public String haha (it it) {
Map Headers=new HashMap<> (a);
Enumeration headerNames=it. GetHeaderNames ();

While (headerNames. HasMoreElements ()) {
String headerName=(String) headerNames. NextElement ();
Headers. The put (headerName, it getHeader (headerName));

}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related