HTTP POST的cURL请求在语法上是不正确的

时间:2013-07-30 22:43:23

标签: rest tomcat curl http-post jersey-client

我试图在Tomcat 7上运行Apache Jersey REST的Windows Server 2008 R2上通过cURL命令行执行以下命令。

curl -X POST -d "<userList xmlns="urn:user"><user role="ROLE_OPERATOR" loginName="test_login1"></user></userList>" -H "Content-Type: application/xml" --basic --user username:password http://localhost:8080/meolutws/UserList/

当我发出命令时,我收到HTTP 400消息:“客户端发送的请求在语法上是不正确的。”

webservice上的方法签名如下所示:

@POST
@CONSUMES({"application/xml"})
public Response createUsers(UserList users){
}

UserList类定义如下:

  @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserList", namespace = "urn:user", propOrder = {
    "users"
})
@XmlRootElement(name = "userList", namespace = "urn:user")
public class UserList
    implements Serializable
{

    @XmlElement(name = "user")
    protected List<User> users;

    public List<User> getUsers() {
        if (users == null) {
            users = new ArrayList<User>();
        }
        return this.users;
    }
}

用户定义如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", namespace = "urn:user")
@XmlRootElement(name = "user", namespace = "urn:user")
public class User
    implements Serializable
{

    @XmlAttribute(name = "loginName", required = true)
    protected String loginName;

    @XmlAttribute(name = "role", required = true)
    protected String role;


    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String value) {
        this.loginName = value;
    }       

    public String getRole() {
        return role;
    }

    public void setRole(String value) {
        this.role = value;
    }
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

引号的使用看起来不对。你这样做:

curl -X POST \
  -d "<userList xmlns="urn:user"><user role="ROLE_OPERATOR" loginName="test_login1"></user></userList>" \
  -H "Content-Type: application/xml" \
  --basic --user meo:meo \
  http://localhost:8080/meolutws/UserList/

你的身体是:

"<userList xmlns="urn:user"><user role="ROLE_OPERATOR" loginName="test_login1"></user></userList>"

您不会转义引号"。如果您使用'

,是否会发生错误
'<userList xmlns="urn:user"><user role="ROLE_OPERATOR" loginName="test_login1"></user></userList>'