springboot如何接收text/plain参数

 更新时间:2025年06月09日 08:37:44   作者:不想码代码的程序员  
这篇文章主要介绍了springboot如何接收text/plain参数的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

springboot接收text/plain参数

Spring MVC在解析参数时

会通过

org.springframework.http.converter.HttpMessageConverter

转换器进行转换,转换器通过

org.springframework.http.converter.HttpMessageConverter#canRead

判断请求的MediaType + 不同实现的字段进行判断。

入口为

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时,接收参数应为String,且需要添加@RequestBody注解。

具体代码如下:

public class TestController {

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

	}
	
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java函数式编程(一):你好,Lambda表达式

    Java函数式编程(一):你好,Lambda表达式

    这篇文章主要介绍了Java函数式编程(一):你好,Lambda表达式,本文讲解了新老函数式编程的一些变化,需要的朋友可以参考下
    2014-09-09
  • 收集的一些常用java正则表达式

    收集的一些常用java正则表达式

    收集的一些常用java正则表达式,需要的朋友可以参考一下
    2013-02-02
  • SpringBoot常用注解@RestControllerAdvice详解

    SpringBoot常用注解@RestControllerAdvice详解

    这篇文章主要介绍了SpringBoot常用注解@RestControllerAdvice详解,@RestControllerAdvice是一个组合注解,由@ControllerAdvice、@ResponseBody组成,而@ControllerAdvice继承了@Component,因此@RestControllerAdvice本质上是个Component,需要的朋友可以参考下
    2024-01-01
  • Java 用反射设置对象的属性值实例详解

    Java 用反射设置对象的属性值实例详解

    这篇文章主要介绍了Java 用反射设置对象的属性值实例详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • SpringBoot项目如何打可执行war包

    SpringBoot项目如何打可执行war包

    最近小编做了一个springboot项目,最后需要打成war包在容器中部署,下面小编给大家分享下SpringBoot项目如何打可执行war包,感兴趣的朋友一起看看吧
    2020-04-04
  • Springboot整合GateWay+Nacos实现动态路由

    Springboot整合GateWay+Nacos实现动态路由

    本文主要介绍了Springboot整合GateWay+Nacos实现动态路由,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-08-08
  • java线程池ThreadPoolExecutor的八种拒绝策略示例详解

    java线程池ThreadPoolExecutor的八种拒绝策略示例详解

    ThreadPoolExecutor是一个典型的缓存池化设计的产物,因为池子有大小,当池子体积不够承载时,就涉及到拒绝策略。JDK中已预设了 4 种线程池拒绝策略,下面结合场景详细聊聊这些策略的使用场景以及还能扩展哪些拒绝策略
    2021-11-11
  • Mybatis自关联查询一对多查询的实现示例

    Mybatis自关联查询一对多查询的实现示例

    这篇文章主要介绍了Mybatis自关联查询一对多查询的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Maven依赖管理之parent与dependencyManagement深入分析

    Maven依赖管理之parent与dependencyManagement深入分析

    首先我们来说说parent标签,其实这个不难解释,就是父的意思,pom也有继承的。比方说我现在有A,B,C,A是B,C的父级。现在就是有一个情况B,C其实有很多jar都是共同的,其实是可以放在父项目里面,这样,让B,C都继承A就方便管理了
    2022-10-10
  • Java爬取网站源代码和链接代码实例

    Java爬取网站源代码和链接代码实例

    这篇文章主要介绍了Java爬取网站源代码和链接代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11

最新评论