本文整理匯總了Java中org.springframework.http.HttpOutputMessage類的典型用法代碼示例。如果您正苦於以下問題:Java HttpOutputMessage類的具體用法?Java HttpOutputMessage怎麽用?Java HttpOutputMessage使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpOutputMessage類屬於org.springframework.http包,在下文中一共展示了HttpOutputMessage類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: writeInternal
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
@Override //Object就是springmvc返回值
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
// 從threadLocal中獲取當前的Request對象
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.currentRequestAttributes()).getRequest();
String callbackParam = request.getParameter(callbackName);
if (StringUtils.isEmpty(callbackParam)) {
// 沒有找到callback參數,直接返回json數據
super.writeInternal(object, outputMessage);
} else {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
try {
//將對象轉換為json串,然後用回調方法包括起來
String result = callbackParam + "(" + super.getObjectMapper().writeValueAsString(object)
+ ");";
IOUtils.write(result, outputMessage.getBody(), encoding.getJavaName());
} catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
}
開發者ID:jthinking,項目名稱:linux-memory-monitor,代碼行數:25,代碼來源:CallbackMappingJackson2HttpMessageConverter.java
示例2: writeInternal
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
JsonGenerator jsonGenerator =
this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
// A workaround for JsonGenerators not applying serialization features
// https://github.com/FasterXML/jackson-databind/issues/12
if (this.objectMapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) {
jsonGenerator.useDefaultPrettyPrinter();
}
try {
if (this.jsonPrefix != null) {
jsonGenerator.writeRaw(this.jsonPrefix);
}
this.objectMapper.writeValue(jsonGenerator, object);
}
catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
示例3: writeInternal
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
Charset charset = this.getCharset(outputMessage.getHeaders());
OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
try {
if(this.jsonPrefix != null) {
writer.append(this.jsonPrefix);
}
this.gson.toJson(o, writer);
writer.close();
} catch (JsonIOException var7) {
throw new HttpMessageNotWritableException("Could not write JSON: " + var7.getMessage(), var7);
}
}
示例4: writeForm
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
private void writeForm(MultiValueMap<String, String> form, MediaType contentType,
HttpOutputMessage outputMessage) throws IOException {
Charset charset;
if (contentType != null) {
outputMessage.getHeaders().setContentType(contentType);
charset = contentType.getCharset() != null ? contentType.getCharset() : this.defaultCharset;
} else {
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
charset = this.defaultCharset;
}
StringBuilder builder = new StringBuilder();
buildByNames(form, charset, builder);
final byte[] bytes = builder.toString().getBytes(charset.name());
outputMessage.getHeaders().setContentLength(bytes.length);
if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(bytes, outputStream));
} else {
StreamUtils.copy(bytes, outputMessage.getBody());
}
}
示例5: writeMultipart
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
private void writeMultipart(final MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage)
throws IOException {
final byte[] boundary = generateMultipartBoundary();
Map<String, String> parameters = Collections.singletonMap("boundary", new String(boundary, "US-ASCII"));
MediaType contentType = new MediaType(mixed, parameters);
HttpHeaders headers = outputMessage.getHeaders();
headers.setContentType(contentType);
if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
streamingOutputMessage.setBody(outputStream -> {
writeParts(outputStream, parts, boundary);
writeEnd(outputStream, boundary);
});
} else {
writeParts(outputMessage.getBody(), parts, boundary);
writeEnd(outputMessage.getBody(), boundary);
}
}
示例6: writePart
import org.springframework.http.HttpOutputMessage; //導入依賴的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: writePart
import org.springframework.http.HttpOutputMessage; //導入依賴的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 : partConverters) {
if (messageConverter.canWrite(partType, partContentType)) {
HttpOutputMessage multipartOutputMessage = new MultipartHttpOutputMessage(os);
multipartOutputMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
if (!partHeaders.isEmpty()) {
multipartOutputMessage.getHeaders().putAll(partHeaders);
}
((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartOutputMessage);
return;
}
}
throw new HttpMessageNotWritableException(
"Could not write request: no suitable HttpMessageConverter found for request type [" +
partType.getName() + "]");
}
示例8: writeInternal
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
@Override
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
InputStream in = resource.getInputStream();
try {
StreamUtils.copy(in, outputMessage.getBody());
}
finally {
try {
in.close();
}
catch (IOException ex) {
}
}
outputMessage.getBody().flush();
}
示例9: handleHttpEntityResponse
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
if (responseEntity == null) {
return;
}
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
}
HttpHeaders entityHeaders = responseEntity.getHeaders();
if (!entityHeaders.isEmpty()) {
outputMessage.getHeaders().putAll(entityHeaders);
}
Object body = responseEntity.getBody();
if (body != null) {
writeWithMessageConverters(body, inputMessage, outputMessage);
}
else {
outputMessage.getBody();
}
}
示例10: writeInternal
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
@Override
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
Charset charset = getCharset(outputMessage.getHeaders());
try (OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset)) {
if (ignoreType) {
gsonForWriter.toJson(o, writer);
return;
}
if (type != null) {
gsonForWriter.toJson(o, type, writer);
return;
}
gsonForWriter.toJson(o, writer);
} catch (JsonIOException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
示例11: encodeRequest
import org.springframework.http.HttpOutputMessage; //導入依賴的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);
}
示例12: writeInternal
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
@Override
protected void writeInternal(CSVHttpResponse response, HttpOutputMessage output) throws IOException {
final Charset charset = getCharset(response);
output.getHeaders().setContentType(createMediaType(charset));
output.getHeaders().set("Content-Disposition", "attachment; filename=\"" + response.getFilename() + "\"");
try (final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(output.getBody(), charset);
final CSVWriter writer = new CSVWriter(outputStreamWriter, ';')) {
if (response.getHeaderRow() != null) {
writer.writeNext(response.getHeaderRow());
}
writer.writeAll(response.getRows());
writer.flush();
}
}
示例13: handleHttpEntityResponse
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
private void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
if (responseEntity == null) {
return;
}
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
}
HttpHeaders entityHeaders = responseEntity.getHeaders();
if (!entityHeaders.isEmpty()) {
outputMessage.getHeaders().putAll(entityHeaders);
}
Object body = responseEntity.getBody();
if (body != null) {
writeWithMessageConverters(body, inputMessage, outputMessage);
}
else {
// flush headers
outputMessage.getBody();
}
}
示例14: handleReturnValueMediaTypeSuffix
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
@Test
public void handleReturnValueMediaTypeSuffix() throws Exception {
String body = "Foo";
MediaType accepted = MediaType.APPLICATION_XHTML_XML;
List<MediaType> supported = Collections.singletonList(MediaType.valueOf("application/*+xml"));
servletRequest.addHeader("Accept", accepted);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(supported);
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
processor.handleReturnValue(body, returnTypeStringProduces, mavContainer, webRequest);
assertTrue(mavContainer.isRequestHandled());
verify(messageConverter).write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
}
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:18,代碼來源:RequestResponseBodyMethodProcessorMockTests.java
示例15: handleReturnValue
import org.springframework.http.HttpOutputMessage; //導入依賴的package包/類
@Test
public void handleReturnValue() throws Exception {
String body = "Foo";
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK);
MediaType accepted = MediaType.TEXT_PLAIN;
servletRequest.addHeader("Accept", accepted.toString());
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertTrue(mavContainer.isRequestHandled());
verify(messageConverter).write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
}