Java 我总是收到一封“信”;“不支持的媒体类型”;使用RestTemplate时出错

Java 我总是收到一封“信”;“不支持的媒体类型”;使用RestTemplate时出错,java,spring,rest,jersey,Java,Spring,Rest,Jersey,所以我在休息方面有困难(我是初学者)。我想把一个多部分表单放到某个URL上。我通过Jersey在web上找到的一个示例实现了这一点,不幸的是,在我的项目中,我应该使用spring framework web客户端。下面是我的工作代码 // setup properties String publishUrl = "http://localhost:8080/url/to/rest/api"; File schemaFile = new File("myfile.xml"); String cat

所以我在休息方面有困难(我是初学者)。我想把一个多部分表单放到某个URL上。我通过Jersey在web上找到的一个示例实现了这一点,不幸的是,在我的项目中,我应该使用spring framework 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());
这个很好用。因此,现在我已经尝试将其转录到SpringWeb客户机上。以下是我当前的代码

//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);
我对此有点困惑,因为在Jersey我将MediaType设置为“MULTIPART\u FORM\u DATA\u TYPE”,而在Spring我将其设置为“MULTIPART\u FORM\u DATA”,所以我不确定为什么这会给我一个不受支持的媒体类型。任何想法都会有帮助

编辑

因此,我将代码更改为不使用.put()方法,因为我使用了错误的方法,更改如下所示

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

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

奇怪的是,文件被上传到服务器,但是我没有从服务器得到任何响应。是否需要设置某种类型的标题才能获得响应或其他什么?当使用Jersey时,我每次都会收到一个响应,所以我想知道Jersey中是否有我不知道的预配置内容。

您使用的
restemplate#put(…)
不正确。第三个参数是URI变量的映射。相反,请使用其中一种方法,在该方法中可以传递带有适当标题的
HttpEntity

我将此put方法更改为
HttpEntity entity=new-HttpEntity(表单,标题);ResponseEntity response=rt.exchange(publishUrl、HttpMethod.PUT、entity、String.class)但我现在收到一条消息说localhost:8080未能响应。@seap这似乎表明您的服务器已关闭。当然不是,这就是为什么我有点困惑的原因,Jersey上传文件很好。@seap您是说您遇到了网络异常吗?您可以发布堆栈跟踪吗?
org.springframework.web.client.ResourceAccessException:PUT请求时发生I/O错误“http://localhost:8080/url/to/rest/api“:localhost:8080响应失败;嵌套的异常是org.apache.http.NoHttpResponseException:localhost:8080未能在org.springframework.web.client.restemplate.doExecute(restemplate.java:543)org.springframework.web.client.restemplate.execute(restemplate.java:489)org.springframework.web.client.restemplate.exchange(restemplate.java:431)响应。
    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)