title: How to set http header in Json response
body: <p>I've a CXF RESTful service which returns both XML and Json format. I need to add a custom http header in the RESTful service. Here's a sample code snippet.</p>
  
  <pre><code>
      @GET
      @Path("/test")
      @Produces("application/xml")
      public Response test(
              @QueryParam("p") String var
      {
          TestRequest req = new TestRequest();
          req.setVar(var);
          TestResponse res = p.getData(req);
          return Response.ok(res).header("Result", res.getResult()).build();
      }
  </code></pre>
  
  <p>The above code shows the XML response which sets the custom http header "Result". I'm able to see the new http header in the response header. So far so good.</p>
  
  <p>Now, here's the Json version which internally calls the testService() method to get the result, then use google Gson API to send the result back. This has been working well, till I decided to return the new header. Here's the code snippet.</p>
  
  <pre><code>
      @GET
      @Path("/test/jsonp")
      public String testJSONP(
              @QueryParam("p") String var,
              @QueryParam("cb") String callBack
      {
          Response resp = test(var);
          XStream xs = new XStream(new JsonHierarchicalStreamDriver());
          xs.setMode(XStream.NO_REFERENCES);
          xs.alias("TestResponse", TestResponse.class);
          StringBuilder sb = new StringBuilder();
          sb.append(callBack);
          sb.append("(");
          GsonBuilder gb = new GsonBuilder();
          gb.registerTypeAdapter(XMLGregorianCalendar.class, new XMLGregorianCalenderSerializer());
          gb.setPrettyPrinting();
          Gson gson = gb.create();
          sb.append(gson.toJson(resp));
          sb.append(")");
          return sb.toString();
      }
  </code></pre>
  
  <p>I'm not able to see the http header in Json response.</p>
  
  <p>Any feedback will be highly appreciated.</p>
  
  <p>-Thanks</p>
  
  <h1>UPDATE</h1>
  
  <p>I added the following code in Json method for my testing.</p>
  
  <pre><code>
      @GET
      @Path("/test/jsonp")
      public String testJSONP(
              @QueryParam("p") String var,
              @QueryParam("cb") String callBack
      {
          Response resp = test(var);
          XStream xs = new XStream(new JsonHierarchicalStreamDriver());
          xs.setMode(XStream.NO_REFERENCES);
          xs.alias("TestResponse", TestResponse.class);
          StringBuilder sb = new StringBuilder();
          sb.append(callBack);
          sb.append("(");
          GsonBuilder gb = new GsonBuilder();
          gb.registerTypeAdapter(XMLGregorianCalendar.class, new XMLGregorianCalenderSerializer());
          gb.setPrettyPrinting();
          Gson gson = gb.create();
          sb.append(gson.toJson(resp));
          sb.append(")");
          return Response.ok(sb.toString(), MediaType.APPLICATION_JSON).header("Result", "50").build();
      }
  </code></pre>
  
  <p>This sets the header value correctly,but the issue is the Json response format seems to have changed. Since this is an existing service, I'm not allowed to do that.
  Here's the existing response format</p>
  
  <pre><code>
  null({
    "status": "Completed",
    "totalResult": "252",
    "bin": [
      {
        "type": "source",
        "value": "documentation",
        "ndocs": "243"
      },
      {
        "type": "source",
        "value": "wikihelp",
        "ndocs": "6"
      },    
    "entries": {
      "item": [
        {
          "url": "http://test.com/test.htm",
          "title": "\u003cspan class\u003d\"vivbold qt0\"\u003eXREF\u003c/span\u003e",
          "snippet": "     Test data.",
          "source": "documentation",
          "type": "html",
          "shortDescription": "Starts the TEST command.",
          "category": [
            "User"
          ],
          "publishDate": "2012-02-05T12:00:00-0500",
          "lastUpdateDate": "2012-03-14T12:00:00-0400",
          "topicId": "GUID-7DD70C3C-B8AD-40F1-8A69-5D1EECEAB013"
        }
      ]
    }
  })
  </code></pre>
  
  <p>Here's the response after adding this change</p>
  
  <pre><code>
  null({
    "status": 200,
    "entity": {
      "status": "Completed",
      "totalResult": "252",
      "bin": [
        {
          "type": "source",
          "value": "documentation",
          "ndocs": "243"
        },
        {
          "type": "source",
          "value": "wikihelp",
          "ndocs": "6"
        }
      ],
      "entries": {
        "item": [
          {
            "url": "http://test.com/test.htm",
            "title": "\u003cspan class\u003d\"vivbold qt0\"\u003eXREF\u003c/span\u003e",
            "snippet": " Test data.",
            "source": "documentation",
            "type": "html",
            "shortDescription": "Starts the TEST command.",
            "category": [
              "User"
            ],
            "publishDate": "2012-02-05T12:00:00-0800",
            "lastUpdateDate": "2012-03-14T12:00:00-0700",
            "topicId": "GUID-7DD70C3C-B8AD-40F1-8A69-5D1EECEAB013"
          }
        ]
      }
    },
    "metadata": {
      "Result": {
  
      }
    }
  })
  </code></pre>