当前位置: 首页>>代码示例>>Java>>正文


Java HttpMessageNotWritableException类代码示例

本文整理汇总了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);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:25,代码来源:MappingJacksonHttpMessageConverter.java

示例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);
  }
}
 
开发者ID:Huawei,项目名称:Server_Management_Common_eSightApi,代码行数:19,代码来源:GsonHttpMessageConverter.java

示例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()
            + "]");
}
 
开发者ID:xm-online,项目名称:xm-ms-entity,代码行数:22,代码来源:MultipartMixedConverter.java

示例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);
    }
}
 
开发者ID:FlowCI,项目名称:flow-platform,代码行数:22,代码来源:RawGsonMessageConverter.java

示例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);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:GsonHttpMessageConverter.java

示例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);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:25,代码来源:AbstractWireFeedHttpMessageConverter.java

示例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);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:MarshallingHttpMessageConverterTests.java

示例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());
}
 
开发者ID:tanhaichao,项目名称:leopard,代码行数:21,代码来源:MappingJackson2HttpMessageConverter.java

示例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();
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-gateway,代码行数:18,代码来源:ProxyExchange.java

示例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();
}
 
开发者ID:DTL-FAIRData,项目名称:FAIRDataPoint,代码行数:17,代码来源:CatalogMetadataConverter.java

示例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();
}
 
开发者ID:DTL-FAIRData,项目名称:FAIRDataPoint,代码行数:17,代码来源:DatasetMetadataConverter.java

示例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();
}
 
开发者ID:DTL-FAIRData,项目名称:FAIRDataPoint,代码行数:18,代码来源:DistributionMetadataConverter.java

示例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() );
}
 
开发者ID:dhis2,项目名称:dhis2-core,代码行数:21,代码来源:JsonPMessageConverter.java

示例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);
}
 
开发者ID:jayqqaa12,项目名称:jbase,代码行数:26,代码来源:JsonPMessageConverter.java


注:本文中的org.springframework.http.converter.HttpMessageNotWritableException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。