如何使用Jersey ContainerResponseFilter读取RESTful服务的响应正文?

时间:2017-05-04 08:01:19

标签: java rest jersey

我正在使用泽西岛,我试图以我的方法访问响应集体,这个问题与此问题非常相似:
如何使用Jersey阅读RESTful服务的响应正文?
身体价值发送给邮递员。

@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();

            }

        }

 }
  

错误:Mai 04,2017 9:52:55 AM   org.apache.catalina.core.StandardWrapperValve调用SCHWERWIEGEND:   Servlet.service()用于servlet [Jersey REST Service]的上下文   path [/ update]抛出异常[java.lang.ClassCastException:   de.beracom.update.UpdateInformation无法转换为   具有根本原因java.lang.ClassCastException的java.io.InputStream]:   de.beracom.update.UpdateInformation无法转换为   java.io.InputStream at   de.beracom.update.LoggingFilter.filter(LoggingFilter.java:92).....

0 个答案:

没有答案