0

I am using Jersey and I am trying to access the responset body in my method very similar to this question:
How do I read response body for a RESTful service using Jersey?
Body value send i with Postman.

@Provider
@PreMatching
public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter
{
    final Logger logger = Logger.getLogger(LoggingFilter.class);

    @Override
        public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
        {
            logger.info("------------------------------------------------------------");
            logger.info("Container-Response");
            logger.info("Status: " + responseContext.getStatus());

            for (Map.Entry<String, List<Object>> entry : responseContext.getHeaders().entrySet())
            {
                logger.info(entry.getKey() + ":" + entry.getValue().toString());
            }

            BufferedReader br;
            try
            {
                br = new BufferedReader(new InputStreamReader((InputStream) responseContext.getEntity(), "UTF-8"));

                        StringBuilder xml = new StringBuilder();
                String line;

                while ((line = br.readLine()) != null)
                {
                    xml.append(line);
                }

                logger.info(xml);
                responseContext.setEntity(new ByteArrayInputStream(xml.toString().getBytes("UTF-8")));
            } catch (IOException e)
            {

                e.printStackTrace();

            }

        }

 }

Error: Mai 04, 2017 9:52:55 AM org.apache.catalina.core.StandardWrapperValve invoke SCHWERWIEGEND: Servlet.service() for servlet [Jersey REST Service] in context with path [/update] threw exception [java.lang.ClassCastException: de.beracom.update.UpdateInformation cannot be cast to java.io.InputStream] with root cause java.lang.ClassCastException: de.beracom.update.UpdateInformation cannot be cast to java.io.InputStream at de.beracom.update.LoggingFilter.filter(LoggingFilter.java:92) .....

Master Po
  • 1,497
  • 1
  • 23
  • 42
Nadin
  • 1
  • 3
  • The entity from `getEntity` is just what you return from your resource method. That's why you are getting the error. Why don't you just use [Jersey's `LoggingFilter/Feature`](https://jersey.java.net/documentation/latest/user-guide.html#logging_chapter) instead of trying to write up your own. – Paul Samsotha May 04 '17 at 08:48
  • How i do this, with LoggingFilter/Feature? – Nadin May 04 '17 at 10:38
  • Just register the `LoggingFeature` with Jersey. Read the docs I linked to for more details – Paul Samsotha May 04 '17 at 10:46
  • I want to read XML (String) and write it to a file log.txt. Do not output to the server with LoggingFeature. – Nadin May 04 '17 at 11:01
  • You can pass your own `java.util.logging.Logger` to the `LoggingFeature` constructor. Look at [the API](https://jersey.java.net/apidocs/2.25.1/jersey/org/glassfish/jersey/logging/LoggingFeature.html). See also [How to write logs in text file when using java.util.logging.Logger](http://stackoverflow.com/q/15758685/2587435) – Paul Samsotha May 04 '17 at 11:15

0 Answers0