我正在尝试通过休息呼叫发布文件。我要POST的文件是一个xml文件。我还需要在标题中包含数据以用于授权目的。通过一些挖掘,看起来我应该将这些标题和文件放入地图中,这可以传递给其余模板的交换方法。这是我的源代码
public class BaseTest {
private RestTemplate restTemplate;
private HttpHeaders headers;
private HttpEntity<String> httpEntity;
private ResponseEntity<String> statusResponse;
@Before
public void setUp() {
restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.set("requester", "test");
headers.set("Authorization", "Token KqY+VEP3A/Cj");
@Test
public void testPost() {
getHeaders().setContentType(MediaType.APPLICATION_XML);
setHttpEntity(new HttpEntity(getHeaders() ));
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new FileSystemResource("C:\\Users\\dt208672\\Desktop\\2104000573.SIM2015060000000000.STMT.980061281_52.xml"));
try {
getRestTemplate().exchange("https://test.com/documents", HttpMethod.POST, new HttpEntity<MultiValueMap<String, Object>>(map, getHeaders()), String.class);
}catch(HttpClientErrorException e) {
fail("Document not found! Status code " + e.getStatusCode());
}
}
}
但是,我收到以下错误。不确定错误的含义。我想我应该将内容类型设置为application / xml文件,因为它是一个xml文件,但不确定是否需要其他配置。有什么建议吗?
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/xml]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:810)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
at com.digitalplatform.test.DocumentTest.testPost(DocumentTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
于2016年3月16日下午9:20在太平洋标准时间编辑: 实际上端点就是这个。这是否意味着我必须改变方法?我可能想避免更改方法中的任何代码..
@RequestMapping(value = "/documents", method = RequestMethod.POST, produces = {
MediaType.APPLICATION_XML_VALUE, AppConstant.APPLICATION_PDF })
@ResponseStatus(HttpStatus.CREATED)
@AuditEventAccess(auditEventAccessSuccess = AppConstant.AUDIT_EVENT_CLASFN_ID_POST_REALTIME_DOCUMENT_SUCCESSFUL, auditEventAccessError = AppConstant.AUDIT_EVENT_CLASFN_ID_POST_REALTIME_DOCUMENT_FAILED)
public <T> T postRealTimeDocument(final HttpServletRequest request) {
答案 0 :(得分:1)
问题是您发布的文件而不是xml格式的字符串。因此,您应该将调用作为multipart / form-data。
实际上你的方法应该如下所示
public class BaseTest {
// as your test
....
@Test
public void testPost() {
getHeaders().setContentType(MediaType.MULTIPART_FORM_DATA);
setHttpEntity(new HttpEntity(getHeaders() ));
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new FileSystemResource("C:\\Users\\dt208672\\Desktop\\2104000573.SIM2015060000000000.STMT.980061281_52.xml"));
try {
getRestTemplate().exchange("https://test.com/documents", HttpMethod.POST, new HttpEntity<MultiValueMap<String, Object>>(map, getHeaders()), String.class);
}catch(HttpClientErrorException e) {
fail("Document not found! Status code " + e.getStatusCode());
}
}
}
导致400错误,因为您的客户端执行了一个mutlipart请求但是没有为此propouse配置您的端点 我每次使用这样的端点都会使用这样的:
使用Apache Commons FileUpload
@RequestMapping(value = "/documents", method = RequestMethod.POST, consumes = "multipart/form-data")
public ResponseEntity saveDocument(@RequestParam("file") MultipartFile file){
...
}
或使用Servlet 3抽象
@RequestMapping(value = "/documents", method = RequestMethod.POST, consumes = "multipart/form-data")
public ResponseEntity saveDocument(@RequestParam("file") Part file){
...
}
或使用dto方法
@RequestMapping(value = "/documents", method = RequestMethod.POST, consumes = "multipart/form-data")
public ResponseEntity saveSocument(@ModelAttribute("file") FileDTO file){
...
}
@Data
class FileDTO {
private MultipartFile file;
// eventualy other proeprties
....
}
即使您使用标准的MultipartFile Spring抽象,Servlet 3 Part抽象也使用像FileDTO这样的dto,关键点是:使用multipart / form-data作为消费者mediaType,具有Apache commons fileupload或servlet 3环境
我希望tihi可以帮助你