最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot如何接收text/plain參數(shù)

 更新時間:2025年06月09日 08:37:44   作者:不想碼代碼的程序員  
這篇文章主要介紹了springboot如何接收text/plain參數(shù)的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

springboot接收text/plain參數(shù)

Spring MVC在解析參數(shù)時

會通過

org.springframework.http.converter.HttpMessageConverter

轉換器進行轉換,轉換器通過

org.springframework.http.converter.HttpMessageConverter#canRead

判斷請求的MediaType + 不同實現(xiàn)的字段進行判斷。

入口為

org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor#readWithMessageConverters
/** spring-webmvc-4.2.6  **/

	/**
	 * Create the method argument value of the expected parameter type by reading
	 * from the given HttpInputMessage.
	 * @param <T> the expected type of the argument value to be created
	 * @param inputMessage the HTTP input message representing the current request
	 * @param param the method parameter descriptor (may be {@code null})
	 * @param targetType the target type, not necessarily the same as the method
	 * parameter type, e.g. for {@code HttpEntity<String>}.
	 * @return the created method argument value
	 * @throws IOException if the reading from the request fails
	 * @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
	 */
	@SuppressWarnings("unchecked")
	protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter param,
			Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

		MediaType contentType;
		boolean noContentType = false;
		try {
			contentType = inputMessage.getHeaders().getContentType();
		}
		catch (InvalidMediaTypeException ex) {
			throw new HttpMediaTypeNotSupportedException(ex.getMessage());
		}
		if (contentType == null) {
			noContentType = true;
			contentType = MediaType.APPLICATION_OCTET_STREAM;
		}

		Class<?> contextClass = (param != null ? param.getContainingClass() : null);
		Class<T> targetClass = (targetType instanceof Class<?> ? (Class<T>) targetType : null);
		if (targetClass == null) {
			ResolvableType resolvableType = (param != null ?
					ResolvableType.forMethodParameter(param) : ResolvableType.forType(targetType));
			targetClass = (Class<T>) resolvableType.resolve();
		}

		HttpMethod httpMethod = ((HttpRequest) inputMessage).getMethod();
		Object body = NO_VALUE;

		try {
			inputMessage = new EmptyBodyCheckingHttpInputMessage(inputMessage);

			for (HttpMessageConverter<?> converter : this.messageConverters) {
				Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
				if (converter instanceof GenericHttpMessageConverter) {
					GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter;
					if (genericConverter.canRead(targetType, contextClass, contentType)) {
						if (logger.isDebugEnabled()) {
							logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
						}
						if (inputMessage.getBody() != null) {
							inputMessage = getAdvice().beforeBodyRead(inputMessage, param, targetType, converterType);
							body = genericConverter.read(targetType, contextClass, inputMessage);
							body = getAdvice().afterBodyRead(body, inputMessage, param, targetType, converterType);
						}
						else {
							body = null;
							body = getAdvice().handleEmptyBody(body, inputMessage, param, targetType, converterType);
						}
						break;
					}
				}
				else if (targetClass != null) {
					if (converter.canRead(targetClass, contentType)) {
						if (logger.isDebugEnabled()) {
							logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
						}
						if (inputMessage.getBody() != null) {
							inputMessage = getAdvice().beforeBodyRead(inputMessage, param, targetType, converterType);
							body = ((HttpMessageConverter<T>) converter).read(targetClass, inputMessage);
							body = getAdvice().afterBodyRead(body, inputMessage, param, targetType, converterType);
						}
						else {
							body = null;
							body = getAdvice().handleEmptyBody(body, inputMessage, param, targetType, converterType);
						}
						break;
					}
				}
			}
		}
		catch (IOException ex) {
			throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
		}

		if (body == NO_VALUE) {
			if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||
					(noContentType && inputMessage.getBody() == null)) {
				return null;
			}
			throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
		}

		return body;
	}

通過

org.springframework.http.converter.HttpMessageConverter#canRead

判斷當前轉換器是否支持當前請求。

當MediaType為text/plain時

支持的轉換器為

org.springframework.http.converter.StringHttpMessageConverter

該轉換器只支持String 對象的轉換,所以當content-type為text/plain時,接收參數(shù)應為String,且需要添加@RequestBody注解。

具體代碼如下:

public class TestController {

	@RequestMapping(consume = Media.TEXT_PLAIN_VALUE)
	public void textPalinRequest(@RequestBody String requestBody){

	}
	
}

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java制作帶界面的聊天工具

    java制作帶界面的聊天工具

    這篇文章主要教大家如何利用java制作帶界面的聊天工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 全面詳解java代碼重構與設計模式

    全面詳解java代碼重構與設計模式

    這篇文章主要為大家介紹了全面詳解java代碼重構與設計模式的全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Java?RabbitMQ的持久化和發(fā)布確認詳解

    Java?RabbitMQ的持久化和發(fā)布確認詳解

    這篇文章主要為大家詳細介紹了RabbitMQ的持久化和發(fā)布確認,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • tk.mybatis擴展通用接口使用詳解

    tk.mybatis擴展通用接口使用詳解

    這篇文章主要介紹了tk.mybatis擴展通用接口使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • Spring?AOP?后置通知修改響應httpstatus方式

    Spring?AOP?后置通知修改響應httpstatus方式

    這篇文章主要介紹了Spring?AOP?后置通知修改響應httpstatus方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java基于ReadWriteLock實現(xiàn)鎖的應用

    Java基于ReadWriteLock實現(xiàn)鎖的應用

    這篇文章主要介紹了Java基于ReadWriteLock實現(xiàn)鎖的應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • 簡單了解JAVA NIO

    簡單了解JAVA NIO

    這篇文章主要介紹了JAVA NIO的的相關資料,文中講解非常細致,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • 詳解使用批處理方式配置Java環(huán)境

    詳解使用批處理方式配置Java環(huán)境

    這篇文章主要介紹了詳解使用批處理方式配置Java環(huán)境,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • 實體類使用@Builder,導致@ConfigurationProperties注入屬性失敗問題

    實體類使用@Builder,導致@ConfigurationProperties注入屬性失敗問題

    這篇文章主要介紹了實體類使用@Builder,導致@ConfigurationProperties注入屬性失敗問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java中去除字符串中所有空格的幾種方法

    Java中去除字符串中所有空格的幾種方法

    這篇文章介紹了Java中去除字符串中所有空格的幾種方法,有需要的朋友可以參考一下
    2013-07-07

最新評論

德安县| 同江市| 平定县| 浦江县| 平邑县| 江源县| 邢台市| 罗江县| 苏尼特左旗| 龙南县| 太仆寺旗| 偏关县| 商丘市| 永新县| 淮滨县| 慈利县| 康乐县| 榆树市| 武陟县| 城市| 龙海市| 马尔康县| 晋中市| 繁峙县| 安泽县| 遵义市| 虹口区| 平和县| 天等县| 聊城市| 灯塔市| 德兴市| 抚顺县| 龙井市| 寿宁县| 湟源县| 牟定县| 日喀则市| 阳城县| 广宁县| 南平市|