1

I am using apache wicket stuff REST, and in a simple API... I would like to get the list of headers and simply display it as debug log.

My problem is I am getting an error java.util.UnknownFormatConversionException

From Code:

@MethodMapping(value="/testSubmit", httpMethod=HttpMethod.POST)
public Object testSubmit() {
    return "OK";
}

To Code:

import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.http.HttpHeaders;

...

@MethodMapping(value="/testSubmit", httpMethod=HttpMethod.POST)
public Object testSubmit(@RequestHeader HttpHeaders headers) {
    // Display request headers here
    return "OK";
}

I think the problem is @RequestHeader, HttpHeaders which are from springframework. If I can get the wicketstuff equivalent for these... I will not get the error.

Any idea on how I could fix these or the wicketstuff equivalent for getting the list of request headers?

Thanks

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Borgy Manotoy
  • 1,960
  • 5
  • 27
  • 42
  • you can get headers public Object testSubmit(@RequestHeader("Accept-Encoding") String encoding) { // Display request headers here return "OK"; } – Naveed Dec 18 '19 at 07:58
  • I need all the headers in the request, I was able to get it thru `HttpServletRequest` then `getHeaderNames()` to get the list of header names.. and iterate this names to get the header values. – Borgy Manotoy Dec 18 '19 at 08:08

1 Answers1

0

Since I only need to display the list of headers in the request, I used HttpServletRequest then call getHeaderNames() to get the header names.

Iterate then the header names to get the header values.

To get the HttpServletRequest in wicketstuff rest, in my case I called it like:
HttpServletRequest request = (HttpServletRequest) getCurrentWebRequest().getContainerRequest();

Note:
getCurrentWebRequest() is from org.wicketstuff.rest.resource.AbstractRestResource, and getContainerRequest() is from org.apache.wicket.request.Request.getContainerRequest()

public static String getHeaderInfo(HttpServletRequest request) {
    StringBuilder sb = new StringBuilder();

    sb.append("\n");
    sb.append("\n[HTTP request headers]\n");

    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();

        sb.append("\"");
        sb.append(headerName);
        sb.append("\": \"");

        Enumeration<String> headers = request.getHeaders(headerName);
        while (headers.hasMoreElements()) {
            String headerValue = headers.nextElement();
            sb.append(headerValue);
            if (headers.hasMoreElements()) {
                sb.append(", ");
            }
        }

        sb.append("\"\n");
    }
    return sb.toString();
}

So, to display the header info: System.out.println(getHeaderInfo(request));

Which is just what I need, I just need this to reflect in my debug log

Hope this helps

Borgy Manotoy
  • 1,960
  • 5
  • 27
  • 42