use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method unsupportedRequestBody.
@Test
public void unsupportedRequestBody() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestResponseBodyController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes("UTF-8"));
request.addHeader("Content-Type", "application/pdf");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(415, response.getStatus());
assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project littlefisher-system by littlefishercoder.
the class WebSpringMvcConfig method configureMessageConverters.
/**
* Description: 消息转换器配置
*
* @param converters converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper om = jacksonConverter.getObjectMapper();
om.setDateFormat(new SimpleDateFormat(DateUtil.DATETIME_FORMAT_1));
om.setLocale(Locale.CHINA);
om.setTimeZone(TimeZone.getTimeZone("GMT+8"));
List<MediaType> mediaTypeList = Lists.newArrayList();
mediaTypeList.add(new MediaType(MediaType.APPLICATION_JSON, Charsets.UTF_8));
mediaTypeList.add(new MediaType(MediaType.TEXT_HTML, Charsets.UTF_8));
jacksonConverter.setSupportedMediaTypes(mediaTypeList);
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charsets.UTF_8);
stringConverter.setWriteAcceptCharset(false);
// 保持以下顺序
// "application/json" "application/*+json"
converters.add(jacksonConverter);
// "application/xml" "text/xml" "application/*+xml"
converters.add(new Jaxb2RootElementHttpMessageConverter());
// "application/xml" "text/xml" "application/*+xml"
converters.add(new SourceHttpMessageConverter<>());
// "application/x-www-form-urlencoded" "multipart/form-data"
converters.add(new AllEncompassingFormHttpMessageConverter());
// "application/octet-stream" "*/*"
converters.add(new ByteArrayHttpMessageConverter());
// "text/plain" "*/*"
converters.add(stringConverter);
// "*/*"
converters.add(new ResourceHttpMessageConverter());
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project OpenClinica by OpenClinica.
the class XformMetaDataService method saveAttachedFiles.
public void saveAttachedFiles(String uri, String dir, String fileName) throws IOException {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(uri, HttpMethod.GET, entity, byte[].class, "1");
if (response.getStatusCode().equals(HttpStatus.OK)) {
FileOutputStream output = new FileOutputStream(new File(dir + File.separator + fileName));
IOUtils.write(response.getBody(), output);
}
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project vorto by eclipse.
the class GenerationDelegateProxyService method generate.
@Override
public GeneratedOutput generate(ModelId modelId, String serviceKey, Map<String, String> requestParams) {
ModelInfo modelResource = modelRepositoryService.getById(modelId);
if (modelResource == null) {
throw new ModelNotFoundException("Model with the given ID does not exist", null);
}
if (modelResource.getType() == ModelType.Datatype || modelResource.getType() == ModelType.Mapping) {
throw new GenerationException("Provided model is neither an information model nor a function block model!");
}
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
Generator generatorEntity = getGenerator(serviceKey);
if (generatorEntity == null) {
throw new GenerationException("Generator with key " + serviceKey + " is not a registered generator");
}
generatorEntity.increaseInvocationCount();
this.registeredGeneratorsRepository.save(generatorEntity);
ResponseEntity<byte[]> entity = restTemplate.getForEntity(generatorEntity.getGenerationEndpointUrl() + attachRequestParams(requestParams), byte[].class, modelId.getNamespace(), modelId.getName(), modelId.getVersion());
return new GeneratedOutput(entity.getBody(), extractFileNameFromHeader(entity), entity.getHeaders().getContentLength());
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project nakadi by zalando.
the class WebConfig method configureMessageConverters.
@Override
protected void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setWriteAcceptCharset(false);
converters.add(new ByteArrayHttpMessageConverter());
converters.add(stringConverter);
converters.add(new ResourceHttpMessageConverter());
converters.add(new SourceHttpMessageConverter<>());
converters.add(mappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
Aggregations