通过Spring定义请求接口非常容器,通过几个注解就可以完成,如下:
创新互联是一家从事企业网站建设、成都网站制作、成都网站建设、外贸营销网站建设、行业门户网站建设、网页设计制作的专业网站制作公司,拥有经验丰富的网站建设工程师和网页设计人员,具备各种规模与类型网站建设的实力,在网站建设领域树立了自己独特的设计风格。自公司成立以来曾独立设计制作的站点上千。
@RestController
@RequestMapping("/demos")
public class DemoController {
@GetMapping("/index")
public Object index() {
return "index" ;
}
}
通过上面的@RestController, @RequestMapping就完成了一个简单的接口定义。
实际Spring Web底层是做了很多的工作,其核心组件有HandlerMapping, HandlerAdapter, ViewResolver等组件。
了解了上面的几个核心组件之后,接下来就是自定义实现上面的核心类,来完成接口的请求处理。
自定义注解,标记Controller类及请求参数:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PackEndpoint {
}
参数标记,用来对接口参数进行注解。
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PackParam {
}
该对象用来保存记录,方法参数由@PackParam注解的参数。
public class PackMethodParameter {
// 用来解析接口参数的名称
private ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer() ;
private String name ;
private Executable executable ;
private int parameterIndex ;
private Class> type ;
public PackMethodParameter(String name, int parameterIndex, Executable executable) {
this.name = name;
this.parameterIndex = parameterIndex ;
this.executable = executable ;
}
public PackMethodParameter(int parameterIndex, Executable executable, Class> type) {
this.parameterIndex = parameterIndex ;
this.executable = executable ;
this.type = type ;
}
public boolean hasParameterAnnotation(Class extends Annotation> clazz) {
Method method = (Method) this.executable ;
Parameter[] parameters = method.getParameters() ;
return parameters[this.parameterIndex].isAnnotationPresent(clazz) ;
}
public String getParameterName() {
String[] parameterNames = parameterNameDiscoverer.getParameterNames((Method) this.executable) ;
return parameterNames[this.parameterIndex] ;
}
}
自定义实现了SpringMVC标准的HandlerMapping,这样在DispatcherServlet中才能够识别。
public class PackHandlerMapping implements HandlerMapping, InitializingBean, ApplicationContextAware {
private ApplicationContext context;
private Mapmapping = new HashMap<>();
@Override
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
String requestPath = request.getRequestURI();
Optionalopt = mapping.entrySet().stream().filter(entry -> entry.getKey().equals(requestPath)).findFirst()
.map(Map.Entry::getValue);
if (opt.isPresent()) {
HandlerExecutionChain executionChain = new HandlerExecutionChain(opt.get()) ;
return executionChain ;
}
return null;
}
// Bean初始化时,从容器中查找所有符合条件的Bean对象,即Bean对象上有@PackEndpoint注解
@Override
public void afterPropertiesSet() throws Exception {
String[] beanNames = context.getBeanNamesForType(Object.class) ;
for (String beanName : beanNames) {
Object bean = this.context.getBean(beanName) ;
Class> clazz = bean.getClass() ;
// 判断当前的Bean上是否有PackEndpoint注解,只对有该注解的类进行处理
if (clazz.getAnnotation(PackEndpoint.class) != null) {
RequestMapping clazzMapping = clazz.getAnnotation(RequestMapping.class) ;
String rootPath = clazzMapping.value()[0] ;
if (clazzMapping != null) {
ReflectionUtils.doWithMethods(clazz, method -> {
RequestMapping nestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class) ;
if (nestMapping != null) {
String nestPath = nestMapping.value()[0] ;
String path = rootPath + nestPath ;
PackMethodHandler handler = new PackMethodHandler(method, bean) ;
mapping.put(path, handler) ;
}
}) ;
}
}
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
// 该类的作用:用来记录接口对应的信息,方法,对应的实例,参数信息
public static class PackMethodHandler {
private Method method;
private Object instance;
private PackMethodParameter[] parameters ;
public Method getMethod(){
return method;
}
public void setMethod(Method method){
this.method = method;
}
public Object getInstance(){
return instance;
}
public void setInstance(Object instance){
this.instance = instance;
}
public PackMethodHandler(Method method, Object instance){
super();
this.method = method;
this.instance = instance;
Parameter[] params = method.getParameters() ;
this.parameters = new PackMethodParameter[params.length] ;
for (int i = 0; i < params.length; i++) {
this.parameters[i] = new PackMethodParameter(i, method, params[i].getType()) ;
}
}
public PackMethodParameter[] getParameter() {
return this.parameters ;
}
}
}
专门用来解析处理接口方法中的参数信息然后从请求中读取。
public interface PackHandlerMethodArgumentResolver {
boolean supportsParameter(PackMethodParameter methodParameter);
Object resolveArgument(PackMethodParameter methodParameter, HttpServletRequest request);
}
public class PackParamHandlerMethodArgumentResolver implements PackHandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(PackMethodParameter methodParameter){
return methodParameter.hasParameterAnnotation(PackParam.class) ;
}
@Override
public Object resolveArgument(PackMethodParameter methodParameter, HttpServletRequest request){
String name = methodParameter.getParameterName() ;
Object arg = null;
String[] parameterValues = request.getParameterValues(name) ;
if (parameterValues != null) {
arg = parameterValues.length == 1 ? parameterValues[0] : parameterValues ;
}
return arg ;
}
}
自定义实现了SpringMVC标准的HandlerAdatper,这样在DispatcherServlet中才能够识别。
public class PackHandlerAdapter implements HandlerAdapter{
@Resource
private ConversionService conversionService ;
private PackParamHandlerMethodArgumentResolver argumentResolver = new PackParamHandlerMethodArgumentResolver() ;
@Override
public boolean supports(Object handler){
return handler instanceof PackMethodHandler;
}
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
PackMethodHandler methodHandler = (PackMethodHandler) handler ;
PackMethodParameter[] parameters = methodHandler.getParameter() ;
Object[] args = new Object[parameters.length] ;
for (int i = 0; i < args.length; i++) {
if (this.argumentResolver.supportsParameter(parameters[i])) {
// 解析对应的方法参数
args[i] = this.argumentResolver.resolveArgument(parameters[i], request) ;
// 类型转换
args[i] = this.conversionService.convert(args[i], parameters[i].getType()) ;
}
}
// 调用目标方法
Object result = methodHandler.getMethod().invoke(methodHandler.getInstance(), args) ;
// 设置响应header,输出内容
response.setHeader("Content-Type", "text/plain;charset=utf8") ;
PrintWriter out = response.getWriter() ;
out.write((String) result) ;
out.flush() ;
out.close() ;
return null ;
}
@Override
public long getLastModified(HttpServletRequest request, Object handler){
return -1 ;
}
}
通过以上的步骤就完成了一个完全自定义SpringMVC核心组件的实现。
网页题目:完全自定义实现SpringMVC核心组件
标题来源:http://www.shufengxianlan.com/qtweb/news33/236033.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联