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

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

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

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

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

會(huì)通過(guò)

org.springframework.http.converter.HttpMessageConverter

轉(zhuǎn)換器進(jìn)行轉(zhuǎn)換,轉(zhuǎn)換器通過(guò)

org.springframework.http.converter.HttpMessageConverter#canRead

判斷請(qǐng)求的MediaType + 不同實(shí)現(xiàn)的字段進(jì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;
	}

通過(guò)

org.springframework.http.converter.HttpMessageConverter#canRead

判斷當(dāng)前轉(zhuǎn)換器是否支持當(dāng)前請(qǐng)求。

當(dāng)MediaType為text/plain時(shí)

支持的轉(zhuǎn)換器為

org.springframework.http.converter.StringHttpMessageConverter

該轉(zhuǎn)換器只支持String 對(duì)象的轉(zhuǎn)換,所以當(dāng)content-type為text/plain時(shí),接收參數(shù)應(yīng)為String,且需要添加@RequestBody注解。

具體代碼如下:

public class TestController {

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

	}
	
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

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

    java制作帶界面的聊天工具

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

    全面詳解java代碼重構(gòu)與設(shè)計(jì)模式

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

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

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

    tk.mybatis擴(kuò)展通用接口使用詳解

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

    Spring?AOP?后置通知修改響應(yīng)httpstatus方式

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

    Java基于ReadWriteLock實(shí)現(xiàn)鎖的應(yīng)用

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

    簡(jiǎn)單了解JAVA NIO

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

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

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

    實(shí)體類使用@Builder,導(dǎo)致@ConfigurationProperties注入屬性失敗問(wèn)題

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

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

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

最新評(píng)論

江源县| 宁蒗| 法库县| 搜索| 邵阳市| 崇左市| 抚松县| 集贤县| 安化县| 纳雍县| 都兰县| 图木舒克市| 横山县| 阿克苏市| 金山区| 驻马店市| 弋阳县| 略阳县| 侯马市| 塘沽区| 钟山县| 曲周县| 邯郸县| 增城市| 靖安县| 玛沁县| 云阳县| 鲁山县| 子长县| 浦县| 确山县| 安康市| 潜山县| 洛隆县| 南川市| 上杭县| 阿坝| 大理市| 体育| 晋城| 新昌县|