Java – do string parameters usually escape automatically in Web services?

Today, I found that a simple% of the string parameters passed from the client to the server can lead to an incorrect request 400

Because I have a basic knowledge of Web services, I don't know if this is normal behavior Did I miss something (was it my responsibility to escape the string?) Or should I look elsewhere?

Customer code:

@WebMethod(operationName = "push",action = "urn:Push")
public boolean push(String msg);

Server code:

@XmlRootElement(name = "push",namespace = "http://ws.something.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "push",namespace = "http://ws.something.com/",propOrder = {"arg0"})
public class Push {

    @XmlElement(name = "arg0")
    private java.lang.String arg0;

    public java.lang.String getArg0() {
        return this.arg0;
    }

    public void setArg0(java.lang.String newArg0)  {
        this.arg0 = newArg0;
    }
}

be careful:

This client / server pair works locally on our development host server, even if there is%. In the string parameter However, it does cause bad request 400 on another host server Therefore, it may be related to the server host environment If so, I would like to suggest the possible reasons for this situation

Solution

It is likely that the default character set is different in your environment Try this sample

String test = new String("%");
    System.out.println(test);
    byte[] byteArray = test.getBytes("UTF-16");
    test = new String(byteArray);
    System.out.println(test);

This is crude, but you will understand how character encoding affects the results

Please check here for more details https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>