0

I want, sometimes, to log the raw data in struts2, but dont know where struts2 storage this info. (The bind of the json data to the beans work, i dont need that).

I have an interceptor that inject functionality to the action and manage the exceptions to draw a log with info of the petition.

The moment that i want to log the client send is when the server fail, in this manage of the error. But dont know where struts2 save the JSON. The other fields, like data of the struts invocation, values of the sesion, atributtes of the request, parameters of inputs... no problem. But the json data, i dont know where is.

I send the JSON from Angular, using httpclient with an observable. Something like this:

/** POST: add a new hero to the server */
addHero(hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, this.httpOptions).pipe(
    tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
    catchError(this.handleError<Hero>('addHero'))
  );
}

Some code i use to get the log:

public static String getValoresRequestToString(HttpServletRequest request)
    {
        String retorno = "";
        Enumeration<String> nombresAtributos = request.getAttributeNames();
        while (nombresAtributos.hasMoreElements())
        {
            String nombreAtributo = nombresAtributos.nextElement();
            retorno = retorno + "\n" + "REQUEST:: " + nombreAtributo + " :" + request.getAttribute(nombreAtributo);
        }

        return retorno;
    }

public static String getValoresParametrosConfiguracionToString(ActionInvocation invocation)
    {
        ActionConfig config = invocation.getProxy().getConfig();
        Map<String, String> parameters = config.getParams();
        return UtilString.mapToString(parameters);
    }

public static String getValoresParametrosToString(ActionInvocation invocation)
    {
        Map<String, Object> parametros = invocation.getInvocationContext().getParameters();
        return UtilString.mapToString(parametros);
    }
Pevi
  • 45
  • 7
  • It depends on how do you send json data, post the code where you send json data to Struts. – Roman C Sep 30 '21 at 12:39
  • I send the json data from Angular using an observable and the HttpClient. Its convert a Class to a json internally. I will put the code in monday (havent got access to the code now). Its something like this: https://angular.io/tutorial/toh-pt6 – Pevi Oct 02 '21 at 06:47
  • If you sending data using the post request with Angular http service then it is in the json format. The content type should be application/json. If you have a json plugin enabled then you can use json interceptor to read data from the request. See [Action to accept dynamic json data from user interface](https://stackoverflow.com/a/16752302/573032) for details. – Roman C Oct 02 '21 at 09:02
  • Yep, i see, i think i must rewrite the interceptor of JSON if i want that data later. I am looking the java file of it and confirm that only populate the fields of the action, not save the data raw in any place. – Pevi Oct 03 '21 at 12:31

0 Answers0