简体   繁体   English

无法写入请求:找不到适合请求类型和内容类型的HttpMessageConverter [application / x-java-serialized-object]

[英]Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object]

I am trying to post an xml request to a third party server and expecting an xml response back. 我试图将xml请求发布到第三方服务器,并期望返回xml响应。

I am here by attaching the source code for configuring Cnfiguration,gateways & channels, Customized Rest Template class and Test Client to execute. 我在这里附上用于配置Cnfiguration,网关和通道,定制的Rest Template类和要执行的Test Client的源代码。 We have various message converters for XML Posting but despite that it is always saying no suitable HTTPMessage Converters 我们有各种用于XML发布的消息转换器,但尽管如此,它总是说没有合适的HTTPMessage转换器

/**** Gateway & Channel Configuration class****/

@Configuration
@MessagingGateway(name = "EntryGateway", defaultRequestChannel = "EntryChannel", defaultRequestTimeout = "2000", defaultReplyChannel = "ExitChannel", defaultReplyTimeout = "2000")
public interface Gateway {


    @Gateway(requestChannel = "EntryChannel", requestTimeout = Constants.REQUEST_TIMEOUT_IN_MILLISECONDS, replyChannel = "ExitChannel", replyTimeout = Constants.RESPONSE_TIMEOUT_IN_MILLISECONDS)
    ReqResMessage sendRequest(ReqResMessage request);


}


import java.util.ArrayList;
import java.util.List;

import org.aopalliance.aop.Advice;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;





@Configuration
@ComponentScan("package_name")
@IntegrationComponentScan("package_name")
@EnableIntegration
@Import(value = ExternalSystemsConfiguration.class)
@PropertySource(value = "classpath:properties", ignoreResourceNotFound = true)
public class Configuration {

    private static final Logger logger = LogbackConfig.getApplicationLogger(POPConfiguration.class);

    @Autowired
    Environment env;

    @Autowired
    ExternalSystemsConfiguration externalSystemsConfiguration;

    /**
     * Entry channel to the messaging system through the gateway.
     * 
     * @return MessageChannel
     */
    @Bean
    public MessageChannel EntryChannel() {
        return new DirectChannel();
    }

    @Bean
    public MessageChannel RequestChannel() {
        return new DirectChannel();
    }

    @Bean
    public MessageChannel ResponseChannel() {
        return new DirectChannel();
    }

    @Bean
    public MessageChannel ExitChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "RequestChannel")
    public MessageHandler cmmHttpGateway() throws Exception {



        logger.debug("Entered Configuration httpGateway() ");
        List<Advice> retryAdvices = new ArrayList<>();
        retryAdvices.add(externalSystemsConfiguration.requestHandlerRetryAdvice());
        HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(env.getProperty("url")
                );


         List<HttpMessageConverter<?>> converters = new ArrayList<>();
            converters.add(marshallingMessageConverter());

        handler.setMessageConverters(converters);
        handler.setOutputChannel(popResponseChannel());
        handler.setRequiresReply(true);
        //handler.setExtractPayload(true);

        // handler.
        handler.setHttpMethod(HttpMethod.POST);

        logger.debug("Exited Configuration httpGateway() ");
        return handler;
    }
    @Bean
    public MarshallingHttpMessageConverter marshallingMessageConverter() {
        return new MarshallingHttpMessageConverter(
            jaxb2Marshaller(),
            jaxb2Marshaller()
        );
    }

    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        System.out.println("jaxb2Marshaller");
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setClassesToBeBound(new Class[]{
                ReqResMessage.class,
                ReqResBody.class,
                ReqResHeader.class,
                Request.class,
                Response.class


        });
        return marshaller;
    }
}





/***** Rest Template Comfiguration ******/
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthOption;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.MalformedChallengeException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.AuthenticationStrategy;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.RestTemplate;


@Configuration
@PropertySource(value ="classpath:integration.properties", ignoreResourceNotFound =true)
public class ExternalSystemsConfiguration {

    @Autowired
    Environment env;


    private static final Logger logger = LogbackConfig.getApplicationLogger(ExternalSystemsConfiguration.class);





    @Bean
    public RequestHandlerRetryAdvice requestHandlerRetryAdvice() {
        logger.debug("Entered RetryConfiguration requestHandlerRetryAdvice()");
        RequestHandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
        RetryTemplate retryTemplate = new RetryTemplate();
        FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
        simpleRetryPolicy.setMaxAttempts(Integer.parseInt(env.getProperty("retryAttempts")));
        fixedBackOffPolicy.setBackOffPeriod(Integer.parseInt(env.getProperty("backOffPolicy")));
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
        retryTemplate.setRetryPolicy(simpleRetryPolicy);
        retryAdvice.setRetryTemplate(retryTemplate);
        return retryAdvice;
    }

    /**
     * This method is used to create rest template with configurable read and connection timeouts.It is used by all http gateways.
     * @return RestTemplate
     */
    @SuppressWarnings("deprecation")
    @Bean
    public RestTemplate getRestTemplate(){

         RestTemplate restTemplate = new RestTemplate();


        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope("test.com", 443), new UsernamePasswordCredentials("testuser", "testpassword"));
         HttpHost target = new HttpHost("test", 443, "https");

         System.out.println("BASE64:"+credentialsProvider.toString());
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);

        // Add AuthCache to the execution context
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credentialsProvider);
        context.setAuthCache(authCache);

        Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/xml");
        List<Header> headers = new ArrayList<>();
                headers.add(header);
         HttpHost proxy = new HttpHost("", "", "http");

         RequestConfig config = RequestConfig.custom()
                 .setProxy(proxy)
                 .build();
         CloseableHttpClient  httpClient = HttpClientBuilder.create().setProxy(proxy).setDefaultHeaders(headers).setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
                                                .setDefaultCredentialsProvider(credentialsProvider).setRedirectStrategy(new DefaultRedirectStrategy() {

                                                    private String[] REDIRECT_METHODS = new String[] { 
                                                        HttpGet.METHOD_NAME, HttpHead.METHOD_NAME 
                                                    };

                                                    @Override
                                                    protected boolean isRedirectable(String method) {
                                                        for (String m : REDIRECT_METHODS) {
                                                            if (m.equalsIgnoreCase(method)) {
                                                                return true;
                                                            }
                                                        }
                                                        return false;
                                                    }
                                                })
                                                .build();



        restTemplate.setRequestFactory(new Conn());
    /*   List<HttpMessageConverter<?>> converters = new ArrayList<>();
            converters.add(marshallingMessageConverter());
            converters.add(new FormHttpMessageConverter());
            converters.add(new StringHttpMessageConverter());
            restTemplate.setMessageConverters(converters);*/
        return restTemplate;
    }



    public class Conn extends SimpleClientHttpRequestFactory {

        public Conn(){
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("", 8080));
            this.setProxy(proxy);
        }
        @Override
        protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
            // TODO Auto-generated method stub
            super.prepareConnection(connection, httpMethod);

            connection.setFollowRedirects(true);

            String userpassword = "testuser" + ":" + "testpassword";
            String encodedAuthorization = Base64.getEncoder().encodeToString(userpassword.getBytes());
            System.out.println("basic-----" + encodedAuthorization);

            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty("Authorization", "Basic " + encodedAuthorization);
            connection.addRequestProperty("content-type", "application/xml");
        }

    }
}

/***** Test Client to Post XML Request in the form of Java object as well as String *****/


 */




@ContextConfiguration(classes = Configuration.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class Test {

    private static Logger logger = LogbackConfig.getApplicationLogger(Test.class);

    @Autowired
    private Gateway cmmGateway;

    @Autowired
    RestTemplate template;

    @Test
    public void testJsonResponse() {
        String xml =     "<?xml version=\"1.0\"?>" 
                +"<Message>"
                + "<Header><NCPDPID>3942100</NCPDPID><SentTime>2016-07-14 06:13:00</SentTime>"
                + "<SenderID>01hw320985</SenderID><MaxRowCount>500</MaxRowCount>"
                + "</Header><Body><Request><Distance>100.00</Distance><LastName>ALLEN</LastName>"
                + "<FirstName></FirstName><Gender></Gender><Phone></Phone><City></City>"
                + "<State></State><LicensedState></LicensedState>" + "<DEA></DEA>"
                + "" + "</Request>"
                + "</Body>" + "</Message>";/*
         ReqResMessage user=null;
        try{
        JAXBContext jaxbContext = JAXBContext.newInstance(ReqResMessage.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        user= (ReqResMessage) jaxbUnmarshaller.unmarshal(is);   
       // System.out.println("REquest****"+user.getBody().getRequest());
        }catch(Exception e){
            e.printStackTrace();
        }*/
        ReqResMessage re = new ReqResMessage();
        ReqResHeader header= new ReqResHeader();
        header.setPharmacyNCPDPID("3942100");
        header.setRowCount("500");
        header.setSenderID("01hw320985");

        Request request = new Request();
        request.setDistance("100.00");
        request.setLastName("ALLEN");
        ReqResBody body = new ReqResBody();
        body.setRequest(request);
        re.setBody(body);
        re.setHeader(header);

        //System.out.println("Before:"+System.currentTimeMillis());
        ReqResMessage response =    cmmGateway.sendRequest(re);

    }

}

Finally i found solution by setting header enricher out put channel to be passed as request channel to httpoutbound gateway. 最后,我找到了解决方案,方法是设置标头扩展器输出通道,将其作为请求通道传递到httpoutbound网关。 Find below code snippet,there i set content-type to application/xml that resolves problem. 查找下面的代码片段,将内容类型设置为application / xml,以解决问题。

@Bean
@Transformer(inputChannel = "RequestChannel", outputChannel = "enricherOutputChannel")
public HeaderEnricher makeEnricher() {

    Map<String, ? extends HeaderValueMessageProcessor<?>> headersToAdd = Collections
        .singletonMap(HttpHeaders.CONTENT_TYPE, new StaticHeaderValueMessageProcessor<>("application/xml"));

    HeaderEnricher enricher = new HeaderEnricher(headersToAdd);
    enricher.setDefaultOverwrite(true);

    return enricher;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestClientException:无法写入请求:找不到适合请求类型的HttpMessageConverter - RestClientException: Could not write request: no suitable HttpMessageConverter found for request type RestTemplate无法编写请求:找不到适合请求类型[java.util.HashMap]的HttpMessageConverter - RestTemplate Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] org.springframework.web.client.RestClientException:无法写入请求:找不到适合请求类型的 HttpMessageConverter - org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type 找不到适合请求类型 [java.util.LinkedHashMap] 和内容类型 [multipart/form-data] 的 HttpMessageConverter - no suitable HttpMessageConverter found for request type [java.util.LinkedHashMap] and content type [multipart/form-data] Java Android Spring:找不到适合请求类型的HttpMessageConverter - java android spring: no suitable HttpMessageConverter found for request type HttpMessageConverter 异常:RestClientException:无法写入请求:找不到合适的 HttpMessageConverter - HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found java 中的 HTTP POST 引发错误 - 找不到合适的 HttpMessageConverter 将请求正文读取到 object02ABBBA2F2A298EBDCC4 类型 - HTTP POST in java throws an error - no suitable HttpMessageConverter found to read request body into object of type class 找不到适用于响应类型[..]和内容类型[application / json]的HttpMessageConverter - No suitable HttpMessageConverter found for response type [..] and content type [application/json] 找不到适合响应类型和内容类型的HttpMessageConverter - no suitable HttpMessageConverter found for response type and content type 无法提取响应:没有找到适合响应类型 class 和内容类型 [text/html] 的 HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter found for response type class and content type [text/html]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM