gpt4 book ai didi

java - 无法写入请求: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object]

转载 作者:行者123 更新时间:2023-12-01 09:36:12 25 4
gpt4 key购买 nike

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

我在这里附上用于配置配置、网关和 channel 、自定义 Rest 模板类和测试客户端的源代码来执行。我们有各种用于 XML Posting 的消息转换器,但尽管如此,它总是说没有合适的 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);

}

}

最佳答案

最后,我通过设置 header 丰富器输出 channel 作为请求 channel 传递到 httpoutbound 网关找到了解决方案。找到下面的代码片段,我将内容类型设置为 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;
}

关于java - 无法写入请求: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38894650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com