本文整理匯總了Java中org.springframework.http.converter.HttpMessageConverter類的典型用法代碼示例。如果您正苦於以下問題:Java HttpMessageConverter類的具體用法?Java HttpMessageConverter怎麽用?Java HttpMessageConverter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpMessageConverter類屬於org.springframework.http.converter包,在下文中一共展示了HttpMessageConverter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: configureMessageConverters
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
/*FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,//保留空的字段
SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
SerializerFeature.WriteNullNumberAsZero);//Number null -> 0
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));*/
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
mediaTypes.add(MediaType.TEXT_HTML);
jacksonConverter.setSupportedMediaTypes(mediaTypes);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
jacksonConverter.setObjectMapper(objectMapper);
converters.add(jacksonConverter);
//converters.add(responseBodyConverter());
}
示例2: getRestTemplate
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
protected RestTemplate getRestTemplate() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
converter.setObjectMapper(mapper);
return new RestTemplate(Collections.<HttpMessageConverter<?>>singletonList(converter));
}
示例3: testZg2Template
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@Test
public void testZg2Template() {
Zg2proRestTemplate z0 = new Zg2proRestTemplate();
assertThat(z0.getErrorHandler()).isInstanceOf(RestTemplateErrorHandler.class);
assertThat(z0.getInterceptors().size()).isGreaterThan(0);
List<ClientHttpRequestInterceptor> lInterceptors = new ArrayList<>();
lInterceptors.add(new LoggingRequestInterceptor());
List<HttpMessageConverter<?>> covs = z0.getMessageConverters();
z0 = new Zg2proRestTemplate(null, lInterceptors);
assertThat(z0).isNotNull();
Zg2proRestTemplate z = new Zg2proRestTemplate(covs, null);
z.setInterceptors(lInterceptors);
assertThat(z.getInterceptors().size()).isGreaterThan(0);
z.setRequestFactory(LoggingRequestFactoryFactory.build());
assertThat(z.getInterceptors().size()).isGreaterThan(0);
assertThat(z.getErrorHandler()).isInstanceOf(RestTemplateErrorHandler.class);
rt.getRestTemplate().setRequestFactory(z.getRequestFactory());
ResponseEntity<String> resp;
resp = rt.getForEntity(MockedControllers.TEST_URL_GET_BLANK_REPLY, String.class);
assertNotNull(resp);
ReturnedStructure rs = rt.getForObject(TEST_URL_GET_STRUCTURE, ReturnedStructure.class);
assertThat(rs.getFieldOne()).isEqualTo(12);
}
示例4: postsEndWithPathParam
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@Test
public void postsEndWithPathParam() {
String result = restTemplate.postForObject(
controllerUrl + "sayhello/{name}",
null,
String.class,
"world");
assertThat(jsonOf(result, String.class), is("hello world"));
List<HttpMessageConverter<?>> convertersOld = restTemplate.getMessageConverters();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(converters);
result = restTemplate.postForObject(
controllerUrl + "sayhello/{name}",
null,
String.class,
"中 國");
assertThat(result, is("hello 中 國"));
restTemplate.setMessageConverters(convertersOld);
}
示例5: configureMessageConverters
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
/**
* 修改自定義消息轉換器
* @param converters 消息轉換器列表
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//調用父類的配置
super.configureMessageConverters(converters);
//創建fastJson消息轉換器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//創建配置類
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回內容的過濾
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
fastConverter.setFastJsonConfig(fastJsonConfig);
//將fastjson添加到視圖消息轉換器列表內
converters.add(fastConverter);
}
示例6: writePart
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException {
Object partBody = partEntity.getBody();
Class<?> partType = partBody.getClass();
HttpHeaders partHeaders = partEntity.getHeaders();
MediaType partContentType = partHeaders.getContentType();
for (HttpMessageConverter<?> messageConverter : this.partConverters) {
if (messageConverter.canWrite(partType, partContentType)) {
HttpOutputMessage multipartMessage = new MultipartMixedConverter.MultipartHttpOutputMessage(os);
multipartMessage.getHeaders().setContentDispositionFormData(name, null);
if (!partHeaders.isEmpty()) {
multipartMessage.getHeaders().putAll(partHeaders);
}
((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage);
return;
}
}
throw new HttpMessageNotWritableException(
"Could not write request: no suitable HttpMessageConverter found for request type [" + partType.getName()
+ "]");
}
示例7: wxApiTemplate
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
/**
* 是否有必要模仿Spring不提供RestTemplate,隻提供RestTemplateBuilder
*
* @return dummy
*/
@Bean(name = WxBeans.WX_API_TEMPLATE_NAME)
public WxApiTemplate wxApiTemplate() {
RestTemplateBuilder builder = new RestTemplateBuilder();
builder = builder.requestFactory(new WxApiHttpRequestFactory(wxProperties))
.errorHandler(new WxResponseErrorHandler());
HttpMessageConverters converters = this.messageConverters.getIfUnique();
List<HttpMessageConverter<?>> converterList = new ArrayList<>();
// 加入默認轉換
converterList.add(new WxHttpInputMessageConverter());
if (converters != null) {
converterList.addAll(converters.getConverters());
builder = builder.messageConverters(Collections.unmodifiableList(converterList));
}
return new WxApiTemplate(builder.build());
}
示例8: beforeBodyWrite
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@Override
public BizRes beforeBodyWrite(BizRes body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) {
String deviceToken = response.getHeaders().getFirst(requestHeaderProperties.getDeviceToken());
if (body instanceof ErrorRes) {
((ErrorRes) body).setVd(deviceToken);
return body;
}
if (body instanceof SuccessRes) {
((SuccessRes) body).setVd(deviceToken);
return body;
}
SuccessRes res = new SuccessRes(body);
res.setVd(deviceToken);
return res;
}
示例9: AbstractZg2proRestTemplate
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
protected AbstractZg2proRestTemplate(SimpleModule sm) {
super();
//converters
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
ObjectMapper jsonMapper = camelToKebabObjectMapper(sm);
messageConverters.add(new MappingJackson2HttpMessageConverter(jsonMapper));
this.setMessageConverters(messageConverters);
//errors handling
setErrorHandler(new RestTemplateErrorHandler());
/*
//interceptors
LoggingRequestInterceptor lri = new LoggingRequestInterceptor();
this.setInterceptors(new ArrayList<>());
this.getInterceptors().add(lri);
this.setRequestFactory(LoggingRequestFactoryFactory.build(lri));
*/
}
示例10: validateRestTemplate
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
public RestTemplate validateRestTemplate(RestTemplate restTemplate) {
boolean containsMappingJackson2HttpMessageConverter = false;
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
containsMappingJackson2HttpMessageConverter = true;
}
}
if (!containsMappingJackson2HttpMessageConverter) {
throw new IllegalArgumentException(
"The RestTemplate does not contain a required " + "MappingJackson2HttpMessageConverter.");
}
return restTemplate;
}
示例11: TwitterEscapingFormHttpMessageConverter
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
TwitterEscapingFormHttpMessageConverter() {
setCharset(Charset.forName("UTF-8"));
List<HttpMessageConverter<?>> partConverters = new ArrayList<>();
partConverters.add(new ByteArrayHttpMessageConverter());
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(
Charset.forName("UTF-8"));
stringHttpMessageConverter.setWriteAcceptCharset(false);
partConverters.add(stringHttpMessageConverter);
partConverters.add(new ResourceHttpMessageConverter());
setPartConverters(partConverters);
}
示例12: configureMessageConverters
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter=new MappingJackson2HttpMessageConverter();
ObjectMapper mapper=jackson2HttpMessageConverter.getObjectMapper();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(sdf);
// 設置輸入時忽略在JSON字符串中存在但Java對象實際沒有的屬性
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//允許key沒有使用雙引號的json
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//注冊枚舉反序列化時候忽略大小寫
mapper.registerModule(JsonUtil.getEnumSimpleModule());
converters.add(jackson2HttpMessageConverter);
super.configureMessageConverters(converters);
}
示例13: configureMessageConverters
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd"));
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
}
示例14: beforeBodyWrite
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
MappingFastJsonValue container = getOrCreateContainer(body);
beforeBodyWriteInternal(container, selectedContentType, returnType, request, response);
return container;
}
示例15: setConverters
import org.springframework.http.converter.HttpMessageConverter; //導入依賴的package包/類
@Autowired
protected void setConverters(HttpMessageConverter<?>[] converters) {
this.mappingJackson2HttpMessageConverter = Arrays.asList(converters).stream().filter(
hmc -> hmc instanceof MappingJackson2HttpMessageConverter).findAny().get();
Assert.assertNotNull(
"the JSON message converter must not be null",
this.mappingJackson2HttpMessageConverter);
}