我正在尝试在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:“客户端发送的请求在语法上是不正确的”。

Web服务上的方法签名看起来像这样:

@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;
    }
}


我想念什么?

最佳答案

引号的用法看起来是错误的。你做这个:

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>'

08-27 14:13