當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。