我一直得到一个"不支持的媒体类型"使用RestTemplate时出错

时间:2014-04-26 21:14:54

标签: java spring rest jersey

所以我遇到了REST问题(我是初学者)。我想将多部分表单放到某个URL。我从网上发现的一个例子中用泽西完成了这个,不幸的是在我的项目中我应该使用spring框架web客户端。以下是我的泽西代码。

// setup properties
String publishUrl = "http://localhost:8080/url/to/rest/api";
File schemaFile = new File("myfile.xml");
String catalogName = "myFileName";

FileInputStream inputStream = new FileInputStream(schemaFile);
String jndiName = "myJNDI";
boolean overwrite = true;
boolean enableXmla = true;

//set up multi part form. 
FormDataMultiPart part = new FormDataMultiPart()
        .field("uploadAnalysis", inputStream, MediaType.MULTIPART_FORM_DATA_TYPE)
        .field("catalogName", catalogName, MediaType.MULTIPART_FORM_DATA_TYPE)
        .field("Datasource", jndiName, MediaType.MULTIPART_FORM_DATA_TYPE)
        .field("overwrite", overwrite ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE)
        .field("xmlaEnabledFlag", enableXmla ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE)
        .field("parameters", "Datasource=" + jndiName, MediaType.MULTIPART_FORM_DATA_TYPE);

// If the import service needs the file name do the following.
part.getField("uploadAnalysis").setContentDisposition(FormDataContentDisposition.name("uploadAnalysis").fileName(schemaFile.getName()).build());

//set up client and create web resource
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("Admin", "password"));

WebResource resource = client.resource(publishUrl);
ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).put(ClientResponse.class, part);

System.out.println(response.getStatus());

这很好用。所以现在我试图将其转录为使用spring web客户端。以下是我目前的代码

//setup properties
String publishUrl = "http://localhost:8080/url/to/rest/api";
File schemaFile = new File("myFile.xml");
String catalogName = "myFileName";
String jndiName = "myJNDI";
boolean overwrite = true;
boolean enableXmla = true;

//setup form
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
form.add("uploadAnalysis", new FileSystemResource(schemaFile));
form.add("catalogName", catalogName);
form.add("Datasource", jndiName);
form.add("overwrite", overwrite ? "true" : "false");
form.add("xmlaEnabledFlag", enableXmla ? "true" : "false");
form.add("parameters", "Datasource=" + jndiName);

//Set up credentials
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("Admin", "password"));

//set up http client
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

//create client request factory using httpcient
HttpComponentsClientHttpRequestFactory fact = new HttpComponentsClientHttpRequestFactory(httpclient);

//create rest template
RestTemplate rt = new RestTemplate(fact);

//create http headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

//use put method. 
rt.put(publishUrl, headers, form);

此代码不起作用,我总是收到以下错误:

 WARN  26-04 14:57:11,988 - PUT request for "http://localhost:8080/url/to/rest/api" resulted in 415 (Unsupported Media Type); invoking error handler

我对此感到有点困惑,因为在Jersey我将MediaType设置为&#34; MULTIPART_FORM_DATA_TYPE&#34;在Spring中,我将它设置为&#34; MULTIPART_FORM_DATA&#34;所以我不确定为什么这会给我一个不受支持的媒体类型。任何想法都会有所帮助。

修改

因此我将代码更改为不使用.put()方法,因为我使用错误,更改看起来像这样。

    HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(form, headers);

    ResponseEntity<String> response = rt.exchange(publishUrl, HttpMethod.PUT, entity, String.class);

然而,在进行这些更改后,我收到了无响应错误

org.springframework.web.client.ResourceAccessException: I/O error on PUT request for "http://localhost:8080/url/to/rest/api":localhost:8080 failed to respond; nested exception is org.apache.http.NoHttpResponseException: localhost:8080 failed to respond
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:543)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:431)

奇怪的是文件上传到服务器,但是我没有从服务器得到任何响应。是否有某种类型的标头我必须设置以获得响应或什么?使用泽西时,我每次都会得到一个回应,所以我想知道是否有一些我不知道的在球衣中预配置的东西。

1 个答案:

答案 0 :(得分:1)

您错误地使用了RestTemplate#put(...)。第三个参数是URI变量的Map。而是使用其中一种RestTemplate#exchange(..)方法,您可以使用适当的标头传递HttpEntity