提问者:小点点

SCM管理器REST POST xml返回415不支持的媒体类型


我正在尝试通过 REST 使用 SCM 管理器 (v1.46) 发布 XML 内容。 从命令行使用 cURL 工作正常:

call curl -XPOST -u scmadmin:scmadmin -H "content-type: application/xml" -d "<users><name>abc</name><active>true</active><password>abc</password><displayName>abc</displayName><mail>abc@abc.com</mail><type>xml</type><lastModified/><creationDate/><admin>false</admin></users>" http://localhost:8080/scm/api/rest/users.xml

并创建用户 ABC。我的 Java 客户端使用 Jersey,从 SCM 管理器收到 415 不支持的媒体类型响应。客户端如下所示:

...

public WebResource getService(String p_url, String p_user, String p_password) {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    client.addFilter(new HTTPBasicAuthFilter(p_user, p_password));
    return client.resource(getBaseURI(p_url));
}

...

public Document postXmlDocument(String p_url, String p_user, String p_password, String p_xml) {
    WebResource service = getService(p_url, p_user, p_password);
    Document xmlDocument = null;
    ClientResponse response = service.accept(MediaType.APPLICATION_XML).post(ClientResponse.class, p_xml); 
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + response.getStatus());
    }
    String output = response.getEntity(String.class);
    System.out.println("Server response : \n");
    System.out.println(output);
    return xmlDocument;
}

其中p_xml获取与 cURL 命令中相同的内容。enogh 不是使用 MediaType.APPLICATION_XML 设置接受的媒体类型吗?使用的球衣有这个Maven坐标:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId> 
    <version>1.12</version>
</dependency>

任何暗示都是好的。斯克


共1个答案

匿名用户

接受只显示您想要返回的类型。您需要设置Content-Type来告诉服务器您发送的是什么类型。如果您不这样做,它只会默认为一些意想不到的类型。例如,如果您发送一个字符串,它可能会默认为Content-Type: text/简单。那里的服务器无法将纯文本转换为您的POJO,因此您会得到415不受支持的媒体类型。

你调用类型(字符串|媒体类型)设置内容类型,或使用标头(字符串,字符串)

service.accept(MediaType.APPLICATION_XML).type("application/xml")..

service.accept(MediaType.APPLICATION_XML).header("Content-Type", "application/xml")...