本文整理汇总了Java中org.springframework.http.converter.HttpMessageNotWritableException类的典型用法代码示例。如果您正苦于以下问题:Java HttpMessageNotWritableException类的具体用法?Java HttpMessageNotWritableException怎么用?Java HttpMessageNotWritableException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpMessageNotWritableException类属于org.springframework.http.converter包,在下文中一共展示了HttpMessageNotWritableException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的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.converter.HttpMessageNotWritableException; //导入依赖的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.converter.HttpMessageNotWritableException; //导入依赖的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: writePart
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的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()
+ "]");
}
示例5: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的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);
}
}
示例6: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
Charset charset = getCharset(outputMessage.getHeaders());
OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
try {
if (this.jsonPrefix != null) {
writer.append(this.jsonPrefix);
}
if (type != null) {
this.gson.toJson(o, type, writer);
}
else {
this.gson.toJson(o, writer);
}
writer.close();
}
catch (JsonIOException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
示例7: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
String wireFeedEncoding = wireFeed.getEncoding();
if (!StringUtils.hasLength(wireFeedEncoding)) {
wireFeedEncoding = DEFAULT_CHARSET.name();
}
MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType != null) {
Charset wireFeedCharset = Charset.forName(wireFeedEncoding);
contentType = new MediaType(contentType.getType(), contentType.getSubtype(), wireFeedCharset);
outputMessage.getHeaders().setContentType(contentType);
}
WireFeedOutput feedOutput = new WireFeedOutput();
try {
Writer writer = new OutputStreamWriter(outputMessage.getBody(), wireFeedEncoding);
feedOutput.output(wireFeed, writer);
}
catch (FeedException ex) {
throw new HttpMessageNotWritableException("Could not write WireFeed: " + ex.getMessage(), ex);
}
}
示例8: writeWithMarshallingFailureException
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Test
public void writeWithMarshallingFailureException() throws Exception {
String body = "<root>Hello World</root>";
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MarshallingFailureException ex = new MarshallingFailureException("forced");
Marshaller marshaller = mock(Marshaller.class);
willThrow(ex).given(marshaller).marshal(eq(body), isA(Result.class));
try {
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
converter.write(body, null, outputMessage);
fail("HttpMessageNotWritableException should be thrown");
}
catch (HttpMessageNotWritableException e) {
assertTrue("Invalid exception hierarchy", e.getCause() == ex);
}
}
示例9: write
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
public void write(Object body, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
outputMessage.getHeaders().setContentType(JSON_MEDIA_TYPE);
// outputMessage.getHeaders().setAccessControlAllowOrigin("*");// FIXME 暂时的写法
if (corsConfig.isEnable()) {
HttpServletRequest request = RequestUtil.getCurrentRequest();
String allowOrigin = corsConfig.getAccessControlAllowOrigin(request);
if (StringUtils.isNotEmpty(allowOrigin)) {
outputMessage.getHeaders().set("Access-Control-Allow-Origin", allowOrigin);
outputMessage.getHeaders().set("Access-Control-Allow-Credentials", "true");
// outputMessage.getHeaders().set("Access-Control-Allow-Methods", "GET, POST");
// outputMessage.getHeaders().set("Access-Control-Allow-Headers", "x_requested_with,content-type");
}
}
// System.err.println("ok");
outputMessage.getBody().write(((String) body).getBytes());
}
示例10: getInputStream
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
public ServletInputStream getInputStream() throws IOException {
Object body = body();
MethodParameter output = new MethodParameter(
ClassUtils.getMethod(BodySender.class, "body"), -1);
ServletOutputToInputConverter response = new ServletOutputToInputConverter(
this.response);
ServletWebRequest webRequest = new ServletWebRequest(this.request, response);
try {
delegate.handleReturnValue(body, output, mavContainer, webRequest);
}
catch (HttpMessageNotWritableException
| HttpMediaTypeNotAcceptableException e) {
throw new IllegalStateException("Cannot convert body", e);
}
return response.getInputStream();
}
示例11: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
protected void writeInternal(CatalogMetadata metadata,
HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
String result;
try {
result = MetadataUtils.getString(metadata, format);
} catch (MetadataException e) {
throw new HttpMessageNotWritableException("", e);
}
OutputStreamWriter writer = new OutputStreamWriter(
outputMessage.getBody(), StandardCharsets.UTF_8);
writer.write(result);
writer.close();
}
示例12: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
protected void writeInternal(DatasetMetadata metadata, HttpOutputMessage
outputMessage)
throws IOException, HttpMessageNotWritableException {
String result;
try {
result = MetadataUtils.getString(metadata, format);
} catch (MetadataException e) {
throw new HttpMessageNotWritableException("", e);
}
OutputStreamWriter writer = new OutputStreamWriter(
outputMessage.getBody(), StandardCharsets.UTF_8);
writer.write(result);
writer.close();
}
示例13: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
protected void writeInternal(DistributionMetadata metadata,
HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
String result;
try {
result = MetadataUtils.getString(metadata, format);
} catch (MetadataException e) {
throw new HttpMessageNotWritableException("", e);
}
OutputStreamWriter writer = new OutputStreamWriter(
outputMessage.getBody(), StandardCharsets.UTF_8);
writer.write(result);
writer.close();
}
示例14: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
protected void writeInternal( RootNode rootNode, HttpOutputMessage outputMessage ) throws IOException, HttpMessageNotWritableException
{
List<String> callbacks = Lists.newArrayList( contextService.getParameterValues( DEFAULT_CALLBACK_PARAMETER ) );
String callbackParam;
if ( callbacks.isEmpty() )
{
callbackParam = DEFAULT_CALLBACK_PARAMETER;
}
else
{
callbackParam = callbacks.get( 0 );
}
rootNode.getConfig().getProperties().put( Jackson2JsonNodeSerializer.JSONP_CALLBACK, callbackParam );
nodeService.serialize( rootNode, "application/json", outputMessage.getBody() );
}
示例15: writeInternal
import org.springframework.http.converter.HttpMessageNotWritableException; //导入依赖的package包/类
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
String text = "";
if (obj instanceof String || obj instanceof Number || obj instanceof Boolean) {
text += obj;
} else {
text = JSON.toJSONString(obj, getFeatures());
}
String call = EffectInteceptor.callBack.get();
if (StringUtils.isNotBlank(call)) {
if(obj instanceof String){
text = call + "(\"" + text + "\")";
}else{
text = call + "(" + text + ")";
}
}
OutputStream out = outputMessage.getBody();
byte[] bytes = text.getBytes(getCharset());
out.write(bytes);
}