0

我正在尝试使用 RESTTemplate 将 XML 正文发布到 HTTP 端点,但是抛出了以下异常 - “org.springframework.web.client.RestClientException:无法提取响应:找不到 Content-Type”。

我已检查并且 XML 文件已成功发布到端点,但是我无法解决此异常 - 显然我做错了什么。我想我可能需要使用 HTTP Mapping Converter ,但我不知道如何实现它。

def post(String jenkinsURL, String username, String apiCredentials, String jobName) {

    RestTemplate rest = new RestTemplate()
    String url = "http://$jenkinsURL//createItem?name=$jobName"
    def jenkinsConfigPath = "src/main/resources/JenkinsConfig.xml"
    def encoding = Base64.getEncoder().encodeToString((username + ":" + apiCredentials).getBytes())
    String xmlConfigFile = jenkinsConfigReader.read(jenkinsConfigPath)

    HttpHeaders headers = new HttpHeaders()
    headers.setContentType(MediaType.APPLICATION_XML)
    headers.add("Authorization", "Basic " + encoding)

    HttpEntity<String> entity = new HttpEntity<String>(xmlConfigFile, headers)

    rest.exchange(url, HttpMethod.POST, entity, String.class)
4

1 回答 1

0

在Java中,我写了一个这样的过滤器:

@Provider
public class CorsFilter implements ContainerResponseFilter
{
    @Override
    public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
            throws IOException
    {
        responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
        responseContext.getHeaders().add(
                "Access-Control-Allow-Headers",
                "origin, content-type, accept, authorization, X-Requested-With");
        responseContext.getHeaders().add("Access-Control-Allow-Credentials", "false");
        responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
        if(requestContext.getMethod().equals("OPTIONS"))
        {
            responseContext.setStatus(200);
        }
    }

}

提供者是一种扩展和定制 JAX-RS 运行时的简单方法。也许这也是您解决问题的一种方式。

于 2017-11-02T09:52:19.623 回答