ObjectMapper om = new ObjectMapper();
        om.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
             //购物车对象转Json
            //对象转Json  写过程
            StringWriter json = new StringWriter();
            try {
                om.writeValue(json, buyCart);
            } catch(IOException e) {
                e.printStackTrace();
            }
            //购物车装进Cookie
            Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, json.toString());
            System.out.println(json);
            System.out.println(json.toString());            //关闭浏览器也要有Cookie
            cookie.setMaxAge(3600 * 24);
            cookie.setPath("/");
            response.addCookie(cookie);
response.addCookie(cookie);这一行提示 An invalid character [34] was present in the Cookie value,我输出了一下json.toString(),结果是:{"items":[{"sku":{"id":553,"skuUpperLimit":5},"amount":1}],"productId":281},格式有错么?
34代表什么意思?怎么去查?

解决方案 »

  1.   

    http://blog.csdn.net/liunian02050328/article/details/77751057,这里面有写
      

  2.   


               URLEncoder.encode(json.toString(), "utf-8");
                //购物车装进Cookie
                Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, json.toString());
           
    按照那篇文章的意思,会提示:
    Unhandled exception: java.io.UnsupportedEncodingException,好像并不支持这样写。
      

  3.   

     Cookie cookie = new Cookie(Constants.BUYCART_COOKIE,  URLEncoder.encode(json.toString(), "utf-8"));,这样也是不行