Jimmysnn
Jimmysnn

Reputation: 593

passing special character in jax-rs webservice

I have two jax-rs webservices: WS1 and WS2.

Also I have an html form that user add some text and when click submit button call WS1 or WS2 and send an xml file via post.

The xml file look like:

<documents>
<document> some text 1 </document>
<document> some text 2 </document>
</documents>

When I call ws1 or ws2 from html form, xml file passes without problem.

But when I try to call ws1 from ws2, I can't send xml file which contains special characters in element "document". (don't send all xml file)

So my question is how can I pass xml file that contains special characters or how can replace all special characters?

Call wb1

 public String sendPost(String value) throws Exception {
            String url = "http://localhost:8084//wb1";
            URL obj = new URL(url);
            HttpURLConnection con =  (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("POST");
            String urlParameters = "xmlinput=" + value;

            // Send post request
            con.setDoOutput(true);
            java.io.BufferedOutputStream wr = new BufferedOutputStream(con.getOutputStream());
            wr.write(urlParameters.getBytes());
            wr.flush();
            wr.close();

            //response
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                response.append(inputLine);
            }
            in.close();


            return response.toString();
        }

wb1:

 @POST
 @Produces("application/xml")
 public String getXml(@FormParam("xmlinput") String xmlinput) throws Exception {
     //some code
 }

EDIT:

for example I have this string:

RT @CEJA: CEJA President @BartoliniMatteo with S&amp;D COMAGRI coordinator @paolodecastro. @EP_Agriculture @TheProgressives http://t.co/65xi4

I want to replace &amp; with black space.

I try with String.replaceAll("\W", " "); but 1) replace only &; etc 2) I want to keep links without modification

Upvotes: 0

Views: 1124

Answers (1)

Ali Cheaito
Ali Cheaito

Reputation: 3856

Try Apache Commons Lang org.apache.commons.lang.StringEscapeUtils#escapeXml for requests, and #unescapeXml for responses.

Upvotes: 1

Related Questions