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

时间:2016-08-11 11:06:48

标签: java spring-integration httpclient resttemplate

我正在尝试将xml请求发布到第三方服务器并期待xml响应。

我在这里附上配置Cnfiguration,gateways& amp;的源代码。通道,自定义Rest模板类和要执行的Test Client。我们有各种用于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);

    }

}

1 个答案:

答案 0 :(得分:1)

最后,通过设置标头更丰富的输出通道作为请求通道到httpoutbound网关,我找到了解决方案。找到下面的代码片段,我将content-type设置为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;
}