Apache WicketStuff REST get list of request headers

432 Views Asked by At

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

1

There are 1 best solutions below

0
On

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