所以,我正在开发一个 java 应用程序,它将使用 API (multipart/form-data 和 json) 发送它时,我收到错误 500: POST request for "https://xxx.xxxx.xx.xxx/xx/ xxxxx”导致 500(内部服务器错误)customresponseerror 说:无法提取响应:没有找到适合响应类型 [java.lang.String] 和内容类型 [text/html;charset=utf-8] 的 HttpMessageConverter
这是我的代码:
@Controller
public class ActionController {
private String interactions;
private HttpHeaders createHeaders( ){
return new HttpHeaders(){
{
String auth = "xxxx@xxxxx" + ":" + "xxxxxxxxx";
byte[] encodedAuth = Base64.encode(
auth.getBytes(Charset.forName("US-ASCII")) );
String authHeader = "Basic " + new String( encodedAuth );
set( "Authorization", authHeader );
}
};
}
@RequestMapping(value = "/createcontent", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> sendCreateContent() throws MalformedURLException, IOException, ParseException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = createHeaders();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(acceptableMediaTypes);
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> cnt = new LinkedMultiValueMap<String, Object>();
ContentDTO contentDTO = new ContentDTO();
contentDTO.setExternal_id("CNT1");
contentDTO.setTemplate_type_id(103);
contentDTO.setChannel_id("CHN1");
contentDTO.setTitle("Conteudo1");
contentDTO.setText("Conteudo teste 1");
RulesDTO rules = new RulesDTO();
SimpleDateFormat publish_date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss-SSS");
java.util.Date pdate = publish_date.parse("2013-12-28 11:18:00-030");
java.sql.Timestamp pubdate = new java.sql.Timestamp(pdate.getTime());
rules.setPublish_date(pubdate);
SimpleDateFormat expiration_date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss-SSS");
java.util.Date edate = expiration_date.parse("2014-12-28 11:18:00-030");
java.sql.Timestamp expdate = new java.sql.Timestamp(edate.getTime());
rules.setExpiration_date(expdate);
rules.setNotify_publish(true);
rules.setNotify_expiration(false);
rules.setHighlihted(true);
contentDTO.setRules(rules);
InteractionsDTO interactions = new InteractionsDTO();
interactions.setAllow_comment(true);
interactions.setAuto_download(false);
contentDTO.setInteractions(interactions);
cnt.add("content", "contentDTO");
cnt.add("file", "text.txt");
HttpEntity<MultiValueMap<String, ContentDTO>> request = new HttpEntity<MultiValueMap<String, ContentDTO>>(httpHeaders);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
FormHttpMessageConverter converter = new FormHttpMessageConverter();
messageConverters.add(converter);
CustomResponseErrorHandler errorHandler = new CustomResponseErrorHandler();
restTemplate.setMessageConverters(messageConverters);
restTemplate.setErrorHandler(errorHandler);
HttpEntity<String> response2 = restTemplate.exchange("https://xxx.xxxx.xx.xxx/xx/xxxx", HttpMethod.POST, request, String.class);
String response3 = null;
return null;
}
}
以防万一有人想查看我的 ContentDTO、InteractionsDTO 和 RulesDTO,它遵循这个结构(上图),并创建了“getter 和 setter”和“超级类的构造函数”。
结构:
content={
"external_id": 1000,
"template_type_id": 103,
"channel_id": 226,
"title": "Título do Conteúdo",
"text": "Descrição do Conteúdo",
"rules": {
"publish_date": "2012-07-20T11:18:00-03:00",
"expiration_date": "2012-08-25T11:18:00-03:00",
"notify_publish": true,
"notify_expiration": false,
"highlighted": true
},
"interactions": {
"allow_comment": true,
"auto_download": false
}
}
有人可以帮助我吗?
编辑:堆栈跟踪
...resulted in 500 (Internal Server Error); invoking error handler
org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:77)
at br.com.g4it.service.CustomResponseErrorHandler.handleError(CustomResponseErrorHandler.java:33)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377)
at br.com.g4it.config.controller.ActionController.sendCreateContent(ActionController.java:361)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
2013-01-15 10:32:17,301 [http-bio-8080-exec-9] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving exception from handler [br.com.g4it.config.controller.ActionController@4594cdd]: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.String] and content type [text/html;charset=utf-8]
2013-01-15 10:32:17,354 [http-bio-8080-exec-9] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to default view 'uncaughtException' for exception of type [org.springframework.web.client.RestClientException]
2013-01-15 10:32:17,354 [http-bio-8080-exec-9] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Exposing Exception as model attribute 'exception'
2013-01-15 10:32:17,354 [http-bio-8080-exec-9] DEBUG org.springframework.web.servlet.DispatcherServlet - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'uncaughtException'; model is {exception=org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.String] and content type [text/html;charset=utf-8]}
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.String] and content type [text/html;charset=utf-8]