java json utf-8_Java MediaType.APPLICATION_JSON_UTF8属性代码示例

该代码段展示了如何从Java方法的@RequestBody或@RequestParam参数中提取Mapping信息,特别是当请求方法类型为POST且consumes指定为'application/json;charset=UTF-8'时,如何处理UTF-8编码。当找不到@RequestMapping注解时,会抛出NoRequestMappingFoundException。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

* Searches {@link org.springframework.web.bind.annotation.RequestMapping RequestMapping}

* annotation on the given method argument and extracts

* If RequestMapping annotation is not found, NoRequestMappingFoundException is thrown.

* {@link org.springframework.http.HttpMethod HttpMethod} type equivalent to

* {@link org.springframework.web.bind.annotation.RequestMethod RequestMethod} type

*

* @param element AnnotatedElement object to be examined.

* @return Mapping object

*/

Mapping extractMapping(AnnotatedElement element) {

Annotation annotation = findMappingAnnotation(element);

String[] urls;

RequestMethod requestMethod;

String consumes;

if (annotation instanceof RequestMapping) {

RequestMapping requestMapping = (RequestMapping) annotation;

requestMethod = requestMapping.method().length == 0

? RequestMethod.GET : requestMapping.method()[0];

urls = requestMapping.value();

consumes = StringHelper.getFirstOrEmpty(requestMapping.consumes());

} else if (annotation instanceof GetMapping) {

requestMethod = RequestMethod.GET;

urls = ((GetMapping) annotation).value();

consumes = StringHelper.getFirstOrEmpty(((GetMapping) annotation).consumes());

} else if (annotation instanceof PostMapping) {

requestMethod = RequestMethod.POST;

urls = ((PostMapping) annotation).value();

consumes = StringHelper.getFirstOrEmpty(((PostMapping) annotation).consumes());

} else if (annotation instanceof PutMapping) {

requestMethod = RequestMethod.PUT;

urls = ((PutMapping) annotation).value();

consumes = StringHelper.getFirstOrEmpty(((PutMapping) annotation).consumes());

} else if (annotation instanceof DeleteMapping) {

requestMethod = RequestMethod.DELETE;

urls = ((DeleteMapping) annotation).value();

consumes = StringHelper.getFirstOrEmpty(((DeleteMapping) annotation).consumes());

} else if (annotation instanceof PatchMapping) {

requestMethod = RequestMethod.PATCH;

urls = ((PatchMapping) annotation).value();

consumes = StringHelper.getFirstOrEmpty(((PatchMapping) annotation).consumes());

} else {

throw new NoRequestMappingFoundException(element);

}

HttpMethod httpMethod = HttpMethod.resolve(requestMethod.name());

String url = StringHelper.getFirstOrEmpty(urls);

MediaType mediaType;

try {

mediaType = MediaType.valueOf(consumes);

} catch (InvalidMediaTypeException exception) {

mediaType = MediaType.APPLICATION_JSON_UTF8;

}

return new Mapping(httpMethod, url, mediaType);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值