本文整理汇总了Java中feign.codec.EncodeException类的典型用法代码示例。如果您正苦于以下问题:Java EncodeException类的具体用法?Java EncodeException怎么用?Java EncodeException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EncodeException类属于feign.codec包,在下文中一共展示了EncodeException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if(object instanceof Pageable) {
Pageable pageable = (Pageable) object;
template.query("page", pageable.getPageNumber() + "");
template.query("size", pageable.getPageSize() + "");
if (pageable.getSort() != null) {
applySort(template, pageable.getSort());
}
}else if(object instanceof Sort){
Sort sort = (Sort)object;
applySort(template, sort);
}else{
delegate.encode(object, bodyType, template);
}
}
示例2: encodeMultipartFormRequest
import feign.codec.EncodeException; //导入依赖的package包/类
/**
* Encodes the request as a multipart form. It can detect a single {@link MultipartFile}, an
* array of {@link MultipartFile}s, or POJOs (that are converted to JSON).
*
* @param formMap
* @param template
* @throws EncodeException
*/
private void encodeMultipartFormRequest(Map<String, ?> formMap, RequestTemplate template) throws EncodeException {
if (formMap == null) {
throw new EncodeException("Cannot encode request with null form.");
}
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
for (Entry<String, ?> entry : formMap.entrySet()) {
Object value = entry.getValue();
if (isMultipartFile(value)) {
map.add(entry.getKey(), encodeMultipartFile((MultipartFile) value));
} else if (isMultipartFileArray(value)) {
encodeMultipartFiles(map, entry.getKey(), Arrays.asList((MultipartFile[]) value));
} else {
map.add(entry.getKey(), encodeJsonObject(value));
}
}
encodeRequest(map, multipartHeaders, template);
}
示例3: encodeMultipartFile
import feign.codec.EncodeException; //导入依赖的package包/类
/**
* Wraps a single {@link MultipartFile} into a {@link HttpEntity} and sets the
* {@code Content-type} header to {@code application/octet-stream}
*
* @param file
* @return
*/
private HttpEntity<?> encodeMultipartFile(MultipartFile file) {
HttpHeaders filePartHeaders = new HttpHeaders();
filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
try {
Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream());
return new HttpEntity<>(multipartFileResource, filePartHeaders);
} catch (IOException ex) {
throw new EncodeException("Cannot encode request.", ex);
}
}
示例4: encodeRequest
import feign.codec.EncodeException; //导入依赖的package包/类
/**
* Calls the conversion chain actually used by
* {@link org.springframework.web.client.RestTemplate}, filling the body of the request
* template.
*
* @param value
* @param requestHeaders
* @param template
* @throws EncodeException
*/
private void encodeRequest(Object value, HttpHeaders requestHeaders, RequestTemplate template) throws EncodeException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders);
try {
Class<?> requestType = value.getClass();
MediaType requestContentType = requestHeaders.getContentType();
for (HttpMessageConverter<?> messageConverter : converters) {
if (messageConverter.canWrite(requestType, requestContentType)) {
((HttpMessageConverter<Object>) messageConverter).write(
value, requestContentType, dummyRequest);
break;
}
}
} catch (IOException ex) {
throw new EncodeException("Cannot encode request.", ex);
}
HttpHeaders headers = dummyRequest.getHeaders();
if (headers != null) {
for (Entry<String, List<String>> entry : headers.entrySet()) {
template.header(entry.getKey(), entry.getValue());
}
}
/*
we should use a template output stream... this will cause issues if files are too big,
since the whole request will be in memory.
*/
template.body(outputStream.toByteArray(), UTF_8);
}
示例5: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {
String contentTypeValue = getContentTypeValue(template.headers());
val contentType = ContentType.of(contentTypeValue);
if (!MAP_STRING_WILDCARD.equals(bodyType) || !processors.containsKey(contentType)) {
delegate.encode(object, bodyType, template);
return;
}
val charset = getCharset(contentTypeValue);
val data = (Map<String, Object>) object;
try {
processors.get(contentType).process(template, charset, data);
} catch (Exception ex) {
throw new EncodeException(ex.getMessage());
}
}
示例6: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if(object instanceof ContinuationToken){
ContinuationToken pageable = (ContinuationToken)object;
template.query("continuationToken", pageable.getToken() + "");
}else{
delegate.encode(object, bodyType, template);
}
}
开发者ID:ElderByte-,项目名称:spring-cloud-starter-bootstrap,代码行数:10,代码来源:ContinuationTokenQueryEncoder.java
示例7: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
jacksonJaxbJsonProvider.writeTo(object, bodyType.getClass(), null, null, APPLICATION_JSON_TYPE, null, outputStream);
template.body(outputStream.toByteArray(), Charset.defaultCharset());
} catch (IOException e) {
throw new EncodeException(e.getMessage(), e);
}
}
示例8: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) {
if (!(bodyType instanceof Class)) {
throw new UnsupportedOperationException(
"JAXB only supports encoding raw types. Found " + bodyType);
}
try {
Marshaller marshaller = jaxbContextFactory.createMarshaller((Class) bodyType);
StringWriter stringWriter = new StringWriter();
marshaller.marshal(object, stringWriter);
template.body(stringWriter.toString());
} catch (JAXBException e) {
throw new EncodeException(e.toString(), e);
}
}
示例9: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) {
try {
JavaType javaType = mapper.getTypeFactory().constructType(bodyType);
template.body(mapper.writerFor(javaType).writeValueAsString(object));
} catch (JsonProcessingException e) {
throw new EncodeException(e.getMessage(), e);
}
}
示例10: okIfEncodeRootCauseHasNoMessage
import feign.codec.EncodeException; //导入依赖的package包/类
@Test
public void okIfEncodeRootCauseHasNoMessage() throws Exception {
server.enqueue(new MockResponse().setBody("success!"));
thrown.expect(EncodeException.class);
TestInterface api = new TestInterfaceBuilder()
.encoder(new Encoder() {
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) {
throw new RuntimeException();
}
}).target("http://localhost:" + server.getPort());
api.body(Arrays.asList("foo"));
}
示例11: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
public void encode(Object requestBody, Type bodyType, RequestTemplate request) throws EncodeException {
if (requestBody != null) {
Class<?> requestType = requestBody.getClass();
Collection<String> contentTypes = request.headers().get(HttpHeaders.CONTENT_TYPE);
MediaType requestContentType = null;
if (contentTypes != null && !contentTypes.isEmpty()) {
String type = contentTypes.iterator().next();
requestContentType = MediaType.valueOf(type);
}
for (HttpMessageConverter<?> messageConverter : this.messageConverters.getObject().getConverters()) {
if (messageConverter.canWrite(requestType, requestContentType)) {
FeignOutputMessage outputMessage = new FeignOutputMessage(request);
try {
@SuppressWarnings("unchecked")
HttpMessageConverter<Object> copy = (HttpMessageConverter<Object>) messageConverter;
copy.write(requestBody, requestContentType, outputMessage);
} catch (IOException ex) {
throw new EncodeException("Error converting request body", ex);
}
request.headers(null);
request.headers(FeignUtils.getHeaders(outputMessage.getHeaders()));
request.body(outputMessage.getOutputStream().toByteArray(), Charset.forName("UTF-8")); // TODO:
return;
}
}
String message = "Could not write request: no suitable HttpMessageConverter " + "found for request type ["
+ requestType.getName() + "]";
if (requestContentType != null) {
message += " and content type [" + requestContentType + "]";
}
throw new EncodeException(message);
}
}
示例12: encode
import feign.codec.EncodeException; //导入依赖的package包/类
/**
* {@inheritDoc }
*/
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if (isFormRequest(bodyType)) {
encodeMultipartFormRequest((Map<String, ?>) object, template);
} else {
encodeRequest(object, jsonHeaders, template);
}
}
示例13: encodeMultipartFiles
import feign.codec.EncodeException; //导入依赖的package包/类
/**
* Fills the request map with {@link HttpEntity}s containing the given {@link MultipartFile}s.
* Sets the {@code Content-type} header to {@code application/octet-stream} for each file.
*
* @param the current request map.
* @param name the name of the array field in the multipart form.
* @param files
*/
private void encodeMultipartFiles(LinkedMultiValueMap<String, Object> map, String name, List<? extends MultipartFile> files) {
HttpHeaders filePartHeaders = new HttpHeaders();
filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
try {
for (MultipartFile file : files) {
Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream());
map.add(name, new HttpEntity<>(multipartFileResource, filePartHeaders));
}
} catch (IOException ex) {
throw new EncodeException("Cannot encode request.", ex);
}
}
示例14: encode
import feign.codec.EncodeException; //导入依赖的package包/类
/**
* Converts objects to an appropriate representation in the template.
*
* @param object what to encode as the request body.
* @param bodyType the type the object should be encoded as. {@code Map<String, ?>}, if form
* encoding.
* @param template the request template to populate.
* @throws feign.codec.EncodeException when encoding failed due to a checked exception.
*/
@Override
public void encode(final Object object, final Type bodyType, final RequestTemplate template)
throws EncodeException {
try {
template.body(mapper.writeValueAsString(object));
} catch (JsonProcessingException e) {
throw new EncodeException(e.getMessage(), e);
}
}
示例15: encode
import feign.codec.EncodeException; //导入依赖的package包/类
@Override
public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if (!bodyType.equals(MultipartFile.class)) {
super.encode(object, bodyType, template);
return;
}
val file = (MultipartFile) object;
val data = singletonMap(file.getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
}