Note that there are some explanatory texts on larger screens.

plurals
  1. POUTF-8 encoding Java/Spring (Multipart/form-data)
    text
    copied!<p>So, I'm developing a java application that will post using API (multipart/form-data and json) When sending it, I get error 500: POST request for "https://xxx.xxxx.xx.xxx/xx/xxxxx" resulted in 500 (Internal Server Error) The customresponseerror says: Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.String] and content type [text/html;charset=utf-8]</p> <p>Here's my code:</p> <pre><code>@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&lt;String&gt; sendCreateContent() throws MalformedURLException, IOException, ParseException { RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders = createHeaders(); List&lt;MediaType&gt; acceptableMediaTypes = new ArrayList&lt;MediaType&gt;(); acceptableMediaTypes.add(MediaType.APPLICATION_JSON); httpHeaders.setAccept(acceptableMediaTypes); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap&lt;String, Object&gt; cnt = new LinkedMultiValueMap&lt;String, Object&gt;(); 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&lt;MultiValueMap&lt;String, ContentDTO&gt;&gt; request = new HttpEntity&lt;MultiValueMap&lt;String, ContentDTO&gt;&gt;(httpHeaders); List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters = new ArrayList&lt;HttpMessageConverter&lt;?&gt;&gt;(); FormHttpMessageConverter converter = new FormHttpMessageConverter(); messageConverters.add(converter); CustomResponseErrorHandler errorHandler = new CustomResponseErrorHandler(); restTemplate.setMessageConverters(messageConverters); restTemplate.setErrorHandler(errorHandler); HttpEntity&lt;String&gt; response2 = restTemplate.exchange("https://xxx.xxxx.xx.xxx/xx/xxxx", HttpMethod.POST, request, String.class); String response3 = null; return null; } } </code></pre> <p>Just in case someone wants to see my ContentDTO, InteractionsDTO and RulesDTO, it follows this structure (above) with "getters and setters" and "constructors of supper class" created.</p> <p>Structure:</p> <pre><code>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 } } </code></pre> <p>Can someone helps me out? </p> <p>Edit: Stacktrace</p> <pre><code>...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] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload