﻿{"id":548,"date":"2018-07-29T21:00:52","date_gmt":"2018-07-29T13:00:52","guid":{"rendered":"https:\/\/www.chenweikang.top\/?p=548"},"modified":"2018-07-31T16:54:11","modified_gmt":"2018-07-31T08:54:11","slug":"%e5%88%a9%e7%94%a8java%e5%8f%8d%e5%b0%84%e6%9c%ba%e5%88%b6%ef%bc%8c%e7%9b%b4%e6%8e%a5%e8%b0%83%e7%94%a8service%e6%9c%8d%e5%8a%a1%ef%bc%8c%e8%b7%b3%e8%bf%87controller","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=548","title":{"rendered":"\u5229\u7528Java\u53cd\u5c04\u673a\u5236\uff0c\u76f4\u63a5\u8c03\u7528Service\u670d\u52a1\uff0c\u8df3\u8fc7Controller"},"content":{"rendered":"<blockquote><p>\u5229\u7528java\u53cd\u5c04\u673a\u5236\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u77e5\u9053\u7c7b\u540d\u7684\u60c5\u51b5\u4e0b\u83b7\u53d6\u8be5\u7c7b\u7684\u5404\u7c7b\u4fe1\u606f\uff0c\u5e76\u4e14\u80fd\u8fdb\u884c\u5b9e\u4f8b\u5316\uff0c\u5b8c\u6210\u5bf9\u5bf9\u8c61\u7684\u53cd\u5c04\u8c03\u7528\u3002\u672c\u4f8b\u662f\u57fa\u4e8e\u4e00\u4e2aspringboot\u7684\u6574\u5408\u4e86dubbo\u7684\u5206\u5e03\u5f0f\u9879\u76ee\uff0c\u6211\u4eec\u5728controller\u505a\u4e00\u4e9b\u6539\u52a8\uff0c\u8df3\u8fc7controller\u76f4\u63a5\u5bf9service\u8fdb\u884c\u53cd\u5c04\u8c03\u7528<\/p>\n<p><a href=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump.png\" class=\"gallery_colorbox\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-549\" src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump.png\"  alt=\"\" width=\"990\" height=\"1233\" srcset=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump.png 990w, https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-241x300.png 241w, https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-768x957.png 768w, https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-822x1024.png 822w\" sizes=\"auto, (max-width: 990px) 100vw, 990px\" \/><\/a><\/p><\/blockquote>\n<h2>1.\u521b\u5efa\u4e00\u4e2aservice.json\u4fdd\u5b58\u670d\u52a1\u5217\u8868\u53ca\u8c03\u7528\u65b9\u6cd5<\/h2>\n<pre class=\"prettyprint linenums\">  [\r\n  {\r\n    \"serviceId\":200,\r\n    \"serviceName\":\"top.cwk.dubbo.api.customer.ICustomerService\",\r\n    \"method\":\"getCustomerById\",\r\n    \"version\":\"0.1\",\r\n    \"group\":\"test\"\r\n  }\r\n]\r\n<\/pre>\n<h2>2.\u5b9a\u4e49\u4e00\u4e2aservletContextListener\u76d1\u542c\u5668\uff0c\u670d\u52a1\u542f\u52a8\u7684\u65f6\u5019\u52a0\u8f7dservice.json\u5e76\u4fdd\u5b58\u4e3akey-map\u683c\u5f0f\uff0c\u5b58\u653e\u5230servletContext<\/h2>\n<pre class=\"prettyprint linenums\">\/**\r\n * @ClassName ServiceJsonBeanListener\r\n * @Description servlet\u76d1\u542c\u5668\uff0c\u521d\u59cb\u5316\u5c06service.json\u8bfb\u53d6\u5230ServletContext\r\n * @Author \u7ef4\u5eb7\r\n * @Date 2018\/7\/26 16:27\r\n * @Version 1.0\r\n **\/\r\n@Configuration\r\npublic class ServiceJsonBeanListener implements ServletContextListener {\r\n\r\n    private final Logger logger = LoggerFactory.getLogger(this.getClass());\r\n    private static Map&lt;Long,ServiceJsonBean&gt; SERVICE_MAP = new HashMap&lt;&gt;();\r\n\r\n    @Override\r\n    public void contextInitialized(ServletContextEvent servletContextEvent) {\r\n        File file = null;\r\n        try {\r\n            file = file = ResourceUtils.getFile(\"classpath:service.json\");\r\n            String jsonStr = BaseUtils.readFileContent(file);\r\n            JSONArray array = JSONArray.fromObject(jsonStr);\r\n            ServiceJsonBean[] arrayBean = (ServiceJsonBean[]) JSONArray.toArray(array,ServiceJsonBean.class);\r\n            List&lt;ServiceJsonBean&gt; list = CollectionUtils.arrayToList(arrayBean);\r\n            try{\r\n                \/\/\u5c06list\u8f6c\u6362\u4e3a\u4ee5serviceId \u4e3a\u4e3b\u952e\u7684Map\r\n                SERVICE_MAP = list.parallelStream().collect(\r\n                        Collectors.toMap(ServiceJsonBean::getServiceId, bean -&gt; bean)\r\n                );\r\n            }catch (IllegalStateException e){\r\n                logger.error(\"service.json \u914d\u7f6e\u51fa\u9519,serviceId\u4e0d\u5f97\u91cd\u590d\uff01\",e);\r\n            }\r\n            servletContextEvent.getServletContext().setAttribute(\"SERVICE_MAP\" , SERVICE_MAP);\r\n            logger.debug(\"\u8bfb\u53d6 service.json : \"+SERVICE_MAP.toString());\r\n        } catch (FileNotFoundException e2) {\r\n            logger.error(\" service.json \u914d\u7f6e\u51fa\u9519,\u627e\u4e0d\u5230\u6587\u4ef6\uff01\",e2);\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void contextDestroyed(ServletContextEvent servletContextEvent) {\r\n\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>3. \u5728controller\u6dfb\u52a0\u4e00\u4e2a\u62e6\u622a\u5668\uff0c\u6307\u5b9a\u8fd9\u4e2a\u8def\u5f84\u7684\u8bf7\u6c42\u5168\u90e8\u8d70\u6211\u4eec\u81ea\u5b9a\u7684\u5904\u7406\u6d41\u7a0b\uff08\u8df3\u8fc7controller\uff09<\/h2>\n<pre class=\"prettyprint linenums\">\/**\r\n * @ClassName RouterRequestInterceptor\r\n * @Description RouterRequestInterceptor\r\n * @Author \u7ef4\u5eb7\r\n * @Date 2018\/7\/26 16:08\r\n * @Version 1.0\r\n **\/\r\npublic class RouterRequestInterceptor implements HandlerInterceptor {\r\n\r\n    @Override\r\n    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {\r\n        return RouterToService.routerService(request,response);\r\n    }\r\n\r\n\r\n    @Override\r\n    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {\r\n\r\n    }\r\n\r\n    @Override\r\n    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {\r\n\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3>3.1 RouterToService\u6839\u636eserviceId\u5339\u914d\u670d\u52a1\uff0c\u7136\u540e\u8fdb\u884c\u53cd\u5c04\u8c03\u7528\u670d\u52a1<\/h3>\n<pre class=\"prettyprint linenums\">\/**\r\n * @ClassName RouterToService\r\n * @Description RouterToService \u5339\u914d\u670d\u52a1\uff0c\u53cd\u5c04\u8c03\u7528\r\n * @Author \u7ef4\u5eb7\r\n * @Date 2018\/7\/28 19:59\r\n * @Version 1.0\r\n **\/\r\npublic class RouterToService {\r\n\r\n    private static Map&lt;Long,ServiceJsonBean&gt; SERVICE_MAP = new HashMap&lt;&gt;();\r\n\r\n    static public final boolean routerService(HttpServletRequest request, HttpServletResponse response) throws IOException,\r\n                                                                                                                ClassNotFoundException,\r\n                                                                                                                InvocationTargetException,\r\n                                                                                                                IllegalAccessException,\r\n                                                                                                                InstantiationException {\r\n        SERVICE_MAP = (Map&lt;Long, ServiceJsonBean&gt;) request.getServletContext().getAttribute(\"SERVICE_MAP\");\r\n        String serviceIdStr = request.getParameter(\"serviceId\");\r\n        ServiceJsonBean serviceJsonBean = null;\r\n        response.setCharacterEncoding(\"UTF-8\");\r\n        PrintWriter printWriter = response.getWriter();\r\n\r\n        if(serviceIdStr!=null){\r\n            Long serviceId = Long.valueOf(serviceIdStr);\r\n            serviceJsonBean = SERVICE_MAP.get(serviceId);\r\n            if(serviceJsonBean==null){\r\n                printWriter.write(\"ERROR_101 : No interface was found based on serviceId=\"+serviceId+\"!\");\r\n                return false;\r\n            }\r\n        }else{\r\n            printWriter.write(\"ERROR_100 : The parameter [serviceId] should not be empty!\");\r\n            return false;\r\n        }\r\n\r\n        System.out.println(serviceJsonBean.toString());\r\n        \/\/\u53cd\u5c04\u83b7\u53d6\u8bf7\u6c42\u7684\u63a5\u53e3\r\n        Class clz = Class.forName(serviceJsonBean.getServiceName());\r\n        \/\/\u6839\u636eclass\u4ecespring\u5bb9\u5668\u83b7\u53d6\u5bf9\u8c61\r\n        Object obj = SpringContextUtil.getBean(clz);\r\n        \/\/\u53cd\u5c04\u83b7\u53d6\u8be5\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5\r\n        Method[] methods = obj.getClass().getDeclaredMethods();\r\n        String methodName = serviceJsonBean.getMethod();\r\n\r\n        boolean findMethod = false;\r\n        \/\/\u904d\u5386\u65b9\u6cd5\u627e\u5230\u8981\u6267\u884c\u7684\u65b9\u6cd5\r\n        for (int i=0 ; i&lt;methods.length ;i++){\r\n            if(methodName.equals(methods[i].getName())){\r\n                Method method = methods[i];\r\n                Class[] parameterTypes = method.getParameterTypes();\r\n                Object object = \"\";\r\n                if(parameterTypes.length == 0){\r\n                    \/\/\u8be5\u65b9\u6cd5\u4e3a\u65e0\u53c2\u65b9\u6cd5\r\n                    object =  method.invoke(obj);\r\n                }else{\r\n                    if(parameterTypes.length == 1){\r\n                        Class parameterType = parameterTypes[0];\r\n                        \/\/\u53c2\u6570\u7c7b\u578b\u7684\u540d\u79f0\r\n                        String simpleName = parameterType.getSimpleName();\r\n                        \/\/\u5f53\u53c2\u6570\u4e3aString\/long\/Integer\/Date\u4e4b\u7c7b\u7684\u65f6\u5019 \u6211\u4eec\u9ed8\u8ba4\u53d6\u53c2\u6570\u540d\u79f0\u4e3adata\r\n                        if(FieldTypeConstants.FIELD_TYPE_MAP.containsKey(simpleName)){\r\n                            \/\/\u8fd9\u91cc\u9ed8\u8ba4\u4e00\u4e2a\u53c2\u6570\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u5b57\u6bb5\u4e3adata \u53ea\u80fd\u586b serviceId=100&amp;data=xxx\r\n                            \/\/\u56e0\u4e3a\u865a\u62df\u673a\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\uff0c\u53c2\u6570\u540d\u79f0\u4f1a\u53d8\u6210arg0..arg1\u4e4b\u7c7b\u7684\r\n                            String paramVal = request.getParameter(\"data\");\r\n                            if(paramVal!=null){\r\n                                object = invokeSimpleParam(obj,method,simpleName,paramVal);\r\n                            }else{\r\n                                object = \"ERROR_003 : The parameter [data] should not be empty!\";\r\n                            }\r\n                        }else{\r\n                            \/\/\u670d\u52a1\u5f53\u53c2\u6570\u4e3a\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u5bf9\u8c61\u65f6\uff0c\u53cd\u5c04\u83b7\u53d6\u5bf9\u8c61\uff0c\u5e76\u5c06request\u7684\u53c2\u6570\u6620\u5c04\u5230\u5bf9\u8c61\u7684\u91cc\u7684\u5b57\u6bb5\r\n                            Object arg = FieldUtil.setFieldValue(parameterType.getTypeName(),paramMapSet(request,parameterType.getTypeName()));\r\n                            object =  method.invoke(obj ,arg);\r\n                        }\r\n                    }else{\r\n                        System.err.println(\"\u6682\u4e0d\u652f\u6301\u63a5\u53e3\u65b9\u6cd5\u591a\u53c2\u6570\u8c03\u7528\uff0c\u5982\u679c\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u8bf7\u5c01\u88c5\u4e3aBO\uff01\");\r\n                        object = \"ERROR_002 : Method parameter too much! Only one parameter is allowed !\";\r\n                    }\r\n                }\r\n                printResult(object , printWriter);\r\n                findMethod = true;\r\n                break;\r\n            }\r\n        }\r\n        if(!findMethod){\r\n            String  result = \"ERROR_001 :No matched method [\"+serviceJsonBean.getMethod()+\"] from service \"+serviceJsonBean.getServiceName()+\"!\";\r\n            printWriter.write(result);\r\n        }\r\n        return false;\r\n    }\r\n\r\n\r\n    \/**\r\n     * \u53c2\u6570\u4e3a\u7b80\u5355\u7c7b\u578b\u7684\u65f6\u5019\r\n     *\/\r\n    static private final Object invokeSimpleParam(Object invObject , Method method , String paramType , String paramValue)  {\r\n        Object object = null;\r\n        try {\r\n            if(FieldTypeConstants.TYPE_STRING.equals(paramType)){\r\n                object =  method.invoke(invObject , paramValue);\r\n            }\r\n            if(FieldTypeConstants.TYPE_LONG.equals(paramType)){\r\n                object =  method.invoke(invObject , Long.parseLong(paramValue));\r\n            }\r\n            if(FieldTypeConstants.TYPE_INT.equals(paramType) || FieldTypeConstants.TYPE_INTEGER.equals(paramType) ){\r\n                object =  method.invoke(invObject , Integer.parseInt(paramValue));\r\n            }\r\n            if(FieldTypeConstants.TYPE_BOOLEAN.equals(paramType)){\r\n                object =  method.invoke(invObject , Boolean.parseBoolean(paramValue));\r\n            }\r\n            if(FieldTypeConstants.TYPE_DATE.equals(paramType)){\r\n                object =  method.invoke(invObject , FieldUtil.parseDate(paramValue));\r\n            }\r\n            if(FieldTypeConstants.TYPE_DOUBLE.equals(paramType)){\r\n                object =  method.invoke(invObject , Double.parseDouble(paramValue));\r\n            }\r\n            if(FieldTypeConstants.TYPE_FLOAT.equals(paramType)){\r\n                object =  method.invoke(invObject , Float.parseFloat(paramValue));\r\n            }\r\n        } catch (IllegalAccessException e) {\r\n            System.err.println(\"IllegalAccessException:\"+e.getMessage());\r\n            object = \"ERROR_000 : IllegalAccessException:\"+e.getMessage();\r\n        } catch (InvocationTargetException e) {\r\n            System.err.println(\"InvocationTargetException:\"+e.getMessage());\r\n            object = \"ERROR_000 : InvocationTargetException:\"+e.getMessage();\r\n        }\r\n        return object;\r\n    }\r\n\r\n    \/**\r\n     * \u5c06request\u8bf7\u6c42\u7684\u53c2\u6570\u5c01\u88c5\u4e3aMap\r\n     * @param request\r\n     * @param paramTypeName\r\n     * @return\r\n     * @throws ClassNotFoundException\r\n     *\/\r\n    static private final Map&lt;String,String&gt; paramMapSet(HttpServletRequest request, String paramTypeName) throws ClassNotFoundException {\r\n        Map&lt;String,String&gt; map = new HashMap&lt;&gt;();\r\n        Field[] fields = Class.forName(paramTypeName).getDeclaredFields();\r\n        for (int i=0 ; i&lt;fields.length ;i++){\r\n            String filedName = fields[i].getName();\r\n            map.put(filedName,request.getParameter(filedName));\r\n        }\r\n        return map;\r\n    }\r\n\r\n    \/**\r\n     * \u8fd4\u56dejson\u683c\u5f0f\u7684\u7ed3\u679c\r\n     * @param object\r\n     * @param printWriter\r\n     *\/\r\n    static private final void printResult(Object object , PrintWriter printWriter){\r\n        String returnTypeName = object.getClass().getSimpleName();\r\n        if(FieldTypeConstants.FIELD_TYPE_MAP.containsKey(returnTypeName)){\r\n            String resStr = \"\";\r\n            if(FieldTypeConstants.TYPE_DATE.equals(returnTypeName)){\r\n                \/\/\u65f6\u95f4\u683c\u5f0f\u9700\u8981\u7279\u6b8a\u8f6c\u6362\r\n                resStr =  FieldUtil.fmtDate((Date)object);\r\n            }else {\r\n                resStr = object.toString();\r\n            }\r\n            printWriter.write(resStr);\r\n        }else{\r\n            JSONObject jsonObject = JSONObject.fromObject(object);\r\n            printWriter.write(jsonObject.toString(4));\r\n        }\r\n    }\r\n\r\n}<\/pre>\n<h3>3.2 SpringContextUtil \u4ece\u5bb9\u5668\u83b7\u53d6bean<\/h3>\n<pre class=\"prettyprint linenums\">\/**\r\n * @ClassName SpringContextUtil\r\n * @Description SpringContextUtil\r\n * @Author \u7ef4\u5eb7\r\n * @Date 2018\/7\/26 18:33\r\n * @Version 1.0\r\n **\/\r\n@Configuration\r\npublic class SpringContextUtil implements ApplicationContextAware {\r\n    private static ApplicationContext applicationContext;\r\n    @Override\r\n    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {\r\n        SpringContextUtil.applicationContext = applicationContext;\r\n    }\r\n\r\n    public static ApplicationContext getCtx() {\r\n        return SpringContextUtil.applicationContext;\r\n    }\r\n\r\n    public static  T getBean(Class t) {\r\n        return SpringContextUtil.applicationContext.getBean(t);\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\n<h3>3.3 FieldUtil\u5de5\u5177\u7c7b\uff0c\u7ed9\u5165\u53c2\u8d4b\u503c<\/h3>\n<pre class=\"prettyprint linenums\">\/**\r\n * @ClassName FieldUtil\r\n * @Description \u53c2\u6570\u8d4b\u503c\r\n * @Author \u7ef4\u5eb7\r\n * @Date 2018\/7\/27 17:38\r\n * @Version 1.0\r\n **\/\r\npublic class FieldUtil {\r\n\r\n    \/**\r\n     * set\u5c5e\u6027\u7684\u503c\u5230Bean\r\n     * @param beanTypeName\r\n     * @param valMap\r\n     *\/\r\n    public static Object setFieldValue(String beanTypeName, Map&lt;String, String&gt; valMap) throws ClassNotFoundException, IllegalAccessException, InstantiationException {\r\n        Class&lt;?&gt; cls = Class.forName(beanTypeName);\r\n        Object bean = cls.newInstance();\r\n        \/\/ \u53d6\u51fabean\u91cc\u7684\u6240\u6709\u65b9\u6cd5\r\n        Method[] methods = cls.getDeclaredMethods();\r\n        Field[] fields = cls.getDeclaredFields();\r\n        for (Field field : fields) {\r\n            try {\r\n                String fieldSetName = parSetName(field.getName());\r\n                if (!checkSetMet(methods, fieldSetName)) {\r\n                    continue;\r\n                }\r\n                Method fieldSetMet = cls.getMethod(fieldSetName, field.getType());\r\n                String value = valMap.get(field.getName());\r\n                if (null != value &amp;&amp; !\"\".equals(value)) {\r\n                    String fieldType = field.getType().getSimpleName();\r\n                    if (FieldTypeConstants.TYPE_STRING.equals(fieldType)) {\r\n                        fieldSetMet.invoke(bean, value);\r\n                    } else if (FieldTypeConstants.TYPE_DATE.equals(fieldType)) {\r\n                        Date temp = parseDate(value);\r\n                        fieldSetMet.invoke(bean, temp);\r\n                    } else if (FieldTypeConstants.TYPE_INTEGER.equals(fieldType) || FieldTypeConstants.TYPE_INT.equals(fieldType)) {\r\n                        Integer intval = Integer.parseInt(value);\r\n                        fieldSetMet.invoke(bean, intval);\r\n                    } else if (FieldTypeConstants.TYPE_LONG.equalsIgnoreCase(fieldType)) {\r\n                        Long temp = Long.parseLong(value);\r\n                        fieldSetMet.invoke(bean, temp);\r\n                    } else if (FieldTypeConstants.TYPE_DOUBLE.equalsIgnoreCase(fieldType)) {\r\n                        Double temp = Double.parseDouble(value);\r\n                        fieldSetMet.invoke(bean, temp);\r\n                    } else if (FieldTypeConstants.TYPE_BOOLEAN.equalsIgnoreCase(fieldType)) {\r\n                        Boolean temp = Boolean.parseBoolean(value);\r\n                        fieldSetMet.invoke(bean, temp);\r\n                    } else if (FieldTypeConstants.TYPE_FLOAT.equalsIgnoreCase(fieldType)) {\r\n                        Float temp = Float.parseFloat(value);\r\n                        fieldSetMet.invoke(bean, temp);\r\n                    } else {\r\n                        System.err.println(\"not supper type\" + fieldType);\r\n                    }\r\n                }\r\n            } catch (Exception e) {\r\n                continue;\r\n            }\r\n        }\r\n        return bean;\r\n    }\r\n\r\n    \/**\r\n     * \u5224\u65ad\u662f\u5426\u5b58\u5728\u67d0\u5c5e\u6027\u7684 set\u65b9\u6cd5\r\n     * @param methods\r\n     * @param fieldSetMet\r\n     * @return boolean\r\n     *\/\r\n    private static boolean checkSetMet(Method[] methods, String fieldSetMet) {\r\n        for (Method met : methods) {\r\n            if (fieldSetMet.equals(met.getName())) {\r\n                return true;\r\n            }\r\n        }\r\n        return false;\r\n    }\r\n    \/**\r\n     * \u5224\u65ad\u662f\u5426\u5b58\u5728\u67d0\u5c5e\u6027\u7684 get\u65b9\u6cd5\r\n     * @param methods\r\n     * @param fieldGetMet\r\n     * @return boolean\r\n     *\/\r\n    private static boolean checkGetMet(Method[] methods, String fieldGetMet) {\r\n        for (Method met : methods) {\r\n            if (fieldGetMet.equals(met.getName())) {\r\n                return true;\r\n            }\r\n        }\r\n        return false;\r\n    }\r\n\r\n    \/**\r\n     * \u62fc\u63a5\u67d0\u5c5e\u6027\u7684 get\u65b9\u6cd5\r\n     * @param fieldName\r\n     * @return String\r\n     *\/\r\n    private static String parGetName(String fieldName) {\r\n        if (null == fieldName || \"\".equals(fieldName)) {\r\n            return null;\r\n        }\r\n        return \"get\" + fieldName.substring(0, 1).toUpperCase()\r\n                + fieldName.substring(1);\r\n    }\r\n    \/**\r\n     * \u62fc\u63a5\u5728\u67d0\u5c5e\u6027\u7684 set\u65b9\u6cd5\r\n     * @param fieldName\r\n     * @return String\r\n     *\/\r\n    private static String parSetName(String fieldName) {\r\n        if (null == fieldName || \"\".equals(fieldName)) {\r\n            return null;\r\n        }\r\n        return \"set\" + fieldName.substring(0, 1).toUpperCase()\r\n                + fieldName.substring(1);\r\n    }\r\n\r\n    \/**\r\n     * \u683c\u5f0f\u5316string\u4e3aDate\r\n     * @param datestr\r\n     * @return date\r\n     *\/\r\n    public static Date parseDate(String datestr) {\r\n        if (null == datestr || \"\".equals(datestr)) {\r\n            return null;\r\n        }\r\n        String fmtstr = null;\r\n        if (datestr.indexOf(':') &gt; 0) {\r\n            fmtstr = \"yyyy-MM-dd HH:mm:ss\";\r\n        } else {\r\n            fmtstr = \"yyyy-MM-dd\";\r\n        }\r\n        SimpleDateFormat sdf = new SimpleDateFormat(fmtstr, Locale.UK);\r\n        try {\r\n            return sdf.parse(datestr);\r\n        } catch (ParseException e) {\r\n            return null;\r\n        }\r\n    }\r\n    \/**\r\n     * \u65e5\u671f\u8f6c\u5316\u4e3aString\r\n     * @param date\r\n     * @return date string\r\n     *\/\r\n    public static String fmtDate(Date date) {\r\n        if (null == date) {\r\n            return null;\r\n        }\r\n        SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\", Locale.SIMPLIFIED_CHINESE);\r\n        return sdf.format(date);\r\n    }\r\n\r\n\r\n}\r\n<\/pre>\n<h3>3.4 \u6700\u540e\u5199\u4e00\u4e2a\u901a\u7528\u7684controller<\/h3>\n<pre class=\"prettyprint linenums\">\/**\r\n * @ClassName RouterController\r\n * @Description RouterController\r\n * @Author \u7ef4\u5eb7\r\n * @Date 2018\/7\/27 14:28\r\n * @Version 1.0\r\n **\/\r\n@RequestMapping(\"\/router\")\r\n@Controller\r\npublic class RouterController {\r\n\r\n    @RequestMapping(\"\/service\")\r\n    public String service(HttpServletRequest request){\r\n        return \"index\";\r\n    }\r\n}\r\n\r\n<\/pre>\n<h2>\u00a04. \u6548\u679c\u5c55\u793a<\/h2>\n<p><strong>\u6b64\u65f6\u62e6\u622a\u5668\u62e6\u622a\u7684\u8def\u5f84\u4e3a\/router\/service\uff0c\u90a3\u4e48\u6211\u4eec\u8bf7\u6c42\u8fd9\u4e2a\u670d\u52a1\u5c31\u4f1a\u6309\u6211\u4eec\u7684\u6d41\u7a0b\u5904\u7406\u3002<\/strong><\/p>\n<p><strong>\u8fd9\u4e2a\u65f6\u5019\u53ea\u8981\u5199\u597d\u4e86\u670d\u52a1\uff0c\u7136\u540e\u5728service.json\u914d\u7f6e\u597dserviceName\u4ee5\u53camethod\uff0c\u7136\u540e\u5728\u8bf7\u6c42\u7684\u65f6\u5019\u643a\u5e26\u4e0aservceId\u4ee5\u53ca\u53c2\u6570\u5c31\u884c\uff0c\u4e0d\u7528\u518d\u53bb\u5199\u4e00\u4e2aController\u4e86\uff01<\/strong><\/p>\n<p><strong><span style=\"color: #800000;\">\u6ce8\u610f \uff1a serviceId\u662f\u5168\u5c40\u552f\u4e00\u7684<\/span><\/strong><\/p>\n<p><a href=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-2.png\" class=\"gallery_colorbox\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-556\" src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-2.png\"  alt=\"\" width=\"1617\" height=\"1013\" srcset=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-2.png 1617w, https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-2-300x188.png 300w, https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-2-768x481.png 768w, https:\/\/www.chenweikang.top\/wp-content\/uploads\/2018\/07\/controllerJump-2-1024x642.png 1024w\" sizes=\"auto, (max-width: 1617px) 100vw, 1617px\" \/><\/a><\/p>\n<h2>5. \u9879\u76ee\u5730\u5740<\/h2>\n<p><a href=\"https:\/\/gitee.com\/god007\/springboot_dubbo\" target=\"_blank\" rel=\"noopener\">springboot\u6574\u5408\u4e86dubbo<\/a><\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=548\">\u5229\u7528Java\u53cd\u5c04\u673a\u5236\uff0c\u76f4\u63a5\u8c03\u7528Service\u670d\u52a1\uff0c\u8df3\u8fc7Controller<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528java\u53cd\u5c04\u673a\u5236\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u77e5\u9053\u7c7b\u540d\u7684\u60c5\u51b5\u4e0b\u83b7\u53d6\u8be5\u7c7b\u7684\u5404\u7c7b\u4fe1\u606f\uff0c\u5e76\u4e14\u80fd\u8fdb\u884c\u5b9e\u4f8b\u5316\uff0c\u5b8c\u6210\u5bf9\u5bf9\u8c61\u7684\u53cd\u5c04\u8c03\u7528\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,3,5],"tags":[60],"class_list":["post-548","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-note","category-website","tag-java"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=548"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/media\/137"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}