自定义messageConverter

This commit is contained in:
felord.cn
2020-11-29 00:30:24 +08:00
parent 5dc5205d9c
commit d6ea7378aa
2 changed files with 85 additions and 20 deletions

View File

@@ -2,8 +2,17 @@ package com.enongm.dianji.payment.wechat.v3;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.http.converter.*; import org.springframework.http.converter.*;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
@@ -16,26 +25,77 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* The type Upload http message converter. * @see AllEncompassingFormHttpMessageConverter
*/ */
final class UploadHttpMessageConverter extends FormHttpMessageConverter { final class ExtensionFormHttpMessageConverter extends FormHttpMessageConverter {
private static final String BOUNDARY = "boundary"; private static final String BOUNDARY = "boundary";
private static final boolean jaxb2Present;
private static final boolean jackson2Present;
private static final boolean jackson2XmlPresent;
private static final boolean jackson2SmilePresent;
private static final boolean gsonPresent;
private static final boolean jsonbPresent;
private final List<HttpMessageConverter<?>> partConverters = new ArrayList<>(); private final List<HttpMessageConverter<?>> partConverters = new ArrayList<>();
static {
ClassLoader classLoader = AllEncompassingFormHttpMessageConverter.class.getClassLoader();
jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder", classLoader);
jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader);
jsonbPresent = ClassUtils.isPresent("javax.json.bind.Jsonb", classLoader);
}
/** /**
* Instantiates a new Upload http message converter. * Instantiates a new Upload http message converter.
*/ */
public UploadHttpMessageConverter() { public ExtensionFormHttpMessageConverter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.partConverters.add(new ByteArrayHttpMessageConverter()); this.partConverters.add(new ByteArrayHttpMessageConverter());
this.partConverters.add(stringHttpMessageConverter); this.partConverters.add(stringHttpMessageConverter);
this.partConverters.add(new ResourceHttpMessageConverter()); this.partConverters.add(new ResourceHttpMessageConverter());
try {
this.partConverters.add(new SourceHttpMessageConverter<>());
} catch (Error err) {
// Ignore when no TransformerFactory implementation is available
}
if (jaxb2Present && !jackson2XmlPresent) {
this.partConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
if (jackson2Present) {
this.partConverters.add(new MappingJackson2HttpMessageConverter());
} else if (gsonPresent) {
this.partConverters.add(new GsonHttpMessageConverter());
} else if (jsonbPresent) {
this.partConverters.add(new JsonbHttpMessageConverter());
}
if (jackson2XmlPresent) {
this.partConverters.add(new MappingJackson2XmlHttpMessageConverter());
}
if (jackson2SmilePresent) {
this.partConverters.add(new MappingJackson2SmileHttpMessageConverter());
}
this.setPartConverters(this.partConverters);
applyDefaultCharset(); applyDefaultCharset();
} }
/** /**
* Apply the configured charset as a default to registered part converters. * Apply the configured charset as a default to registered part converters.
*/ */
@@ -50,6 +110,7 @@ final class UploadHttpMessageConverter extends FormHttpMessageConverter {
} }
} }
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void write(MultiValueMap<String, ?> map, @Nullable MediaType contentType, HttpOutputMessage outputMessage) public void write(MultiValueMap<String, ?> map, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
@@ -190,6 +251,7 @@ final class UploadHttpMessageConverter extends FormHttpMessageConverter {
os.write('\r'); os.write('\r');
os.write('\n'); os.write('\n');
} }
private static class MultipartHttpOutputMessage implements HttpOutputMessage { private static class MultipartHttpOutputMessage implements HttpOutputMessage {
private final OutputStream outputStream; private final OutputStream outputStream;

View File

@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestOperations; import org.springframework.web.client.RestOperations;
@@ -30,6 +31,7 @@ import java.util.function.Consumer;
*/ */
public class WechatPayClient { public class WechatPayClient {
private final SignatureProvider signatureProvider; private final SignatureProvider signatureProvider;
private RestOperations restOperations;
/** /**
* Instantiates a new Wechat pay service. * Instantiates a new Wechat pay service.
@@ -38,6 +40,7 @@ public class WechatPayClient {
*/ */
public WechatPayClient(SignatureProvider signatureProvider) { public WechatPayClient(SignatureProvider signatureProvider) {
this.signatureProvider = signatureProvider; this.signatureProvider = signatureProvider;
applyDefaultRestTemplate();
} }
@@ -50,7 +53,7 @@ public class WechatPayClient {
* @return the executor * @return the executor
*/ */
public <M> Executor<M> withType(WechatPayV3Type wechatPayV3Type, M m) { public <M> Executor<M> withType(WechatPayV3Type wechatPayV3Type, M m) {
return new Executor<>(wechatPayV3Type, m, this.signatureProvider); return new Executor<>(wechatPayV3Type, m, this.signatureProvider,this.restOperations);
} }
@@ -61,10 +64,10 @@ public class WechatPayClient {
*/ */
public static class Executor<M> { public static class Executor<M> {
/** /**
* The V 3 pay type. * The V3 pay type.
*/ */
private final WechatPayV3Type wechatPayV3Type; private final WechatPayV3Type wechatPayV3Type;
private RestOperations restOperations; private final RestOperations restOperations;
private final SignatureProvider signatureProvider; private final SignatureProvider signatureProvider;
private final M model; private final M model;
@@ -85,14 +88,13 @@ public class WechatPayClient {
* @param model the model * @param model the model
* @param signatureProvider the signature provider * @param signatureProvider the signature provider
*/ */
public Executor(WechatPayV3Type wechatPayV3Type, Executor(WechatPayV3Type wechatPayV3Type,
M model, M model,
SignatureProvider signatureProvider) { SignatureProvider signatureProvider, RestOperations restOperations) {
this.wechatPayV3Type = wechatPayV3Type; this.wechatPayV3Type = wechatPayV3Type;
this.model = model; this.model = model;
this.signatureProvider = signatureProvider; this.signatureProvider = signatureProvider;
this.restOperations = restOperations;
applyDefaultRestTemplate();
} }
/** /**
@@ -128,16 +130,6 @@ public class WechatPayClient {
this.doExecute(this.header(wechatRequestEntity)); this.doExecute(this.header(wechatRequestEntity));
} }
private void applyDefaultRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
DefaultResponseErrorHandler errorHandler = new WechatPayResponseErrorHandler();
restTemplate.setErrorHandler(errorHandler);
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
// upload
messageConverters.add(new UploadHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
this.restOperations = restTemplate;
}
/** /**
* 构造私钥签名. * 构造私钥签名.
@@ -215,4 +207,15 @@ public class WechatPayClient {
} }
private void applyDefaultRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
DefaultResponseErrorHandler errorHandler = new WechatPayResponseErrorHandler();
restTemplate.setErrorHandler(errorHandler);
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
messageConverters.removeIf(httpMessageConverter -> httpMessageConverter instanceof AllEncompassingFormHttpMessageConverter);
messageConverters.add(new ExtensionFormHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
this.restOperations = restTemplate;
}
} }