【枚举类型】Restful API请求--转换String为枚举类型
IBaseEnum.java
public interface IBaseEnum {
public String getName();
}
FuncEnum.java
import com.ssslinppp.enumConvert.IBaseEnum;
public enum FuncEnum implements IBaseEnum {
AVG("avg", "func-avg"),
MAX("max", "func-max"),
MIN("min", "func-min"),
SUM("sum", "func-sum"),
LAST("last", "func-last");
private String name;
private String desc;
FuncEnum(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
StringToBaseEnumConverterFactory.java
package com.ssslinppp.enumConvert;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
/**
* Description: 将String转换为枚举类型
*/
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, IBaseEnum> {
@Override
public <T extends IBaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
if (!targetType.isEnum()) {
throw new UnsupportedOperationException("只支持转换到枚举类型");
}
return new StringToBaseEnumConverter(targetType);
}
private class StringToBaseEnumConverter<T extends IBaseEnum> implements Converter<String, T> {
private final Class<T> enumType;
public StringToBaseEnumConverter(Class<T> enumType) {
this.enumType = enumType;
}
@Override
public T convert(String s) {
for (T t : enumType.getEnumConstants()) {
if (s.equals(t.getName())) {
return t;
}
}
return null;
}
}
}
注册自定义的ConverterFactory
MyWebAppConfigurer.java
package com.ssslinppp.configs;
import com.ssslinppp.enumConvert.StringToBaseEnumConverterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* Description:注册自定义的ConverterFactory
*/
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(new StringToBaseEnumConverterFactory());
}
}
测试
EnumModel.java
package com.ssslinppp.model;
import com.ssslinppp.enumConvert.baseEnumImpl.FuncEnum;
public class EnumModel {
private FuncEnum funcEnum;
public FuncEnum getFuncEnum() {
return funcEnum;
}
public void setFuncEnum(FuncEnum funcEnum) {
this.funcEnum = funcEnum;
}
}
同时测试@RequestBody和@RequestParam
EnumController.java 测试类
package com.ssslinppp.controller;
import com.ssslinppp.enumConvert.baseEnumImpl.FuncEnum;
import com.ssslinppp.model.EnumModel;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/enum")
public class EnumController {
@RequestMapping("/print")
public String printEnums(@RequestBody EnumModel enumModel, @RequestParam("pathEnum") FuncEnum pathEnum) {
System.out.println("[" + enumModel.getFuncEnum().getName() + ":" + enumModel.getFuncEnum().getDesc() + "]");
System.out.println("[" + pathEnum.getName() + ":" + pathEnum.getDesc() + "]");
return "[" + pathEnum.getName() + ":" + pathEnum.getDesc() + "]";
}
}
请求URL
localhost:8080/enum/print?pathEnum=max
method: post
mediaType:application
请求体:
{
"funcEnum": "AVG"
}
测试结果(output):
[avg:func-avg]
[max:func-max]
【枚举类型】Restful API请求--转换String为枚举类型的更多相关文章
- httpclient连接池在ES Restful API请求中的应用
package com.wm.utils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http ...
- kylin 使用RESTful API 请求
目前根据Kylin的官方文档介绍,Kylin的认证是basic authentication,加密算法是Base64,在POST的header进行用户认证我使用的用户和密码是(格式:username: ...
- 使用 SpringBoot 构建一个RESTful API
目录 背景 创建 SpringBoot 项目/模块 SpringBoot pom.xml api pom.xml 创建 RESTful API 应用 @SpringBootApplication @C ...
- python之restful api(flask)获取数据
需要用到谷歌浏览器的扩展程序 Advanced Rest Client进行模拟请求 1.直接上代码 from flask import Flask from flask import request ...
- SpringBoot实现JWT保护前后端分离RESTful API
通常情况下, 将api直接暴露出来是非常危险的. 每一个api呼叫, 用户都应该附上额外的信息, 以供我们认证和授权. 而JWT是一种既能满足这样需求, 而又简单安全便捷的方法. 前端login获取J ...
- Openstack创建虚拟机 Restful api和RPC调用
Horizon前台界面用于接受用户的输入或动作(action),然后将这些参数构造成RESTful API(https://developer.openstack.org/api-ref/comput ...
- golang interface类型转string等其他类型
inter 是interface类型,转化为string类型是: str := inter .(string) 转为其他类型也类似
- 03.枚举和string以及int类型之间的转换
练习1: 将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...
- 使用WebApiClient请求和管理Restful Api
前言 本篇文章的内容是WebApiClient应用说明篇,如果你没有了解过WebApiClient,可以先阅读以下相关文章: WebApi client 的面向切面编程 我来给.Net设计一款Http ...
随机推荐
- Java中如何正确的将byte[]数组转化为String类型?
很多人在编程时,总是喜欢用一下方法将数组转为字符串:(a为byte数组) String s=a.toString(); 可是每次返回的时候,新手看来返回的结果是乱码,比如说我,写RSA算法时,没有注意 ...
- 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)
链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- Codeforces 766C:Mahmoud and a Message(DP)
题目链接:http://codeforces.com/problemset/problem/766/C 题意 有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母 ...
- JavaSE笔记
this关键字 哪个对象调用方法,方法定义中的this即为该对象的引用! static关键字 使用static声名的成员变量为静态成员变量,在第一次使用的时候被初始化,static成员变量只有一份 使 ...
- CentOS7安装备忘
======1 下载CentOS镜像文件:https://www.centos.org/download/http://isoredirect.centos.org/centos/7/isos/x86 ...
- AtCoder Grand Contest 031 B - Reversi
https://atcoder.jp/contests/agc031/tasks/agc031_b B - Reversi Time Limit: 2 sec / Memory Limit: 1024 ...
- hdu4292 Food 最大流
You, a part-time dining service worker in your college’s dining hall, are now confused with a new pr ...
- nginx负载均衡算法
配置方式 NGINX配置负载均衡主要是在nginx.conf文件中里upstream模块 1.upstream模块应放于nginx.conf配置的http{}标签内2.upstream模块默认算法是w ...
- oracle修改字符集方法
查看源数据库字符集 在sql命令行执行,即可查看 cat exp.dmp |od -x|head -1|awk '{print $2 $3}'|cut -c 3-6 例如我的返回结果为0362,对照以 ...
- skipper backend 负载均衡配置
skipper 对于后端是支持负载均衡处理的,支持官方文档并没有提供,实际使用中,这个还是比较重要的 同时支持健康检查. 格式 hello_lb_group: Path("/foo" ...