3.5 配置Tomcat服务
打开Run/Debug Configuration, 点击左上角的加号,选择Tomcat Server->Local。
HTTP prot默认为8080,若已被使用则改为其他的端口。
选择artifacts,点击右下角的Fix按钮,跳转到Deployment标签,选择刚刚配置的flylolo-readcode。
保存并启动项目,访问UserController,地址:http://localhost:8099/flylolo_readcode/user
3.6 添加json解析:
如果只是返回String类型是没问题了,但大多数需要返回的时候Json类型。
新建一个User类:
package cn.flylolo.model; import lombok.Data; /** * @author FlyLolo * @date 2021/10/11 11:18 */ @Data public class User { private String userId; private String userName; }
这里用到了lombok,需要在build.gradle中添加引用。
implementation 'org.projectlombok:lombok:1.18.20' annotationProcessor 'org.projectlombok:lombok:1.18.20'
注意需要添加第二行,否则在调用对应的get和set方法的时候会出现 “错误: 找不到符号”的错误。
在UserController中添加新的方法:
@GetMapping("/{userId}") public User getName(@PathVariable String userId){ User user = new User(); user.setUserId(userId); user.setUserName(userId + "的名字"); return user; }
将返回一个User对象。
访问http://localhost:8099/flylolo_readcode/user/testid,返回了406,不可接收错误。
因为返回Json类型,需要添加对应的message-converters
,本例采用FastJson。用下面代码替换springmvc.xml中的<mvc:annotation-driven />
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 配置Fastjson支持 --> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
这需要在build.gradle中添加FastJson的引用:
implementation 'com.alibaba:fastjson:1.2.78'
再次访问http://localhost:8099/flylolo_readcode/user/testid,得到了期望的结果。
至此,源码阅读环境准备完毕。
4. 遇到的问题
4.1 gradle进行build的时候,中文出现乱码:
Help->Edit Custom VM Options, 添加如下代码:
-Dfile.encoding=UTF-8
4.2 gradle项目,用了lombok,调用setXXX提示“找不到符号"的错误,需在build.gradle中做如下方式引用
//添加annotationProcessor,否则会出现找不到符号的错误
annotationProcessor 'org.projectlombok:lombok:1.18.20'
implementation 'org.projectlombok:lombok:1.18.20'
//添加annotationProcessor,否则会出现找不到符号的错误 annotationProcessor 'org.projectlombok:lombok:1.18.20' implementation 'org.projectlombok:lombok:1.18.20'
4.3 服务启动报错问题
服务无法正常启动,报错“org.apache.tomcat.util.modeler.BaseModelMBean.invoke 调用方法[manageApp]时发生异常 java.lang.IllegalStateException: 启动子级时出错”,详细错误如下:
Connected to server [2021-10-11 03:30:50,531] Artifact flylolo-readcode: Artifact is being deployed, please wait... 11-Oct-2021 15:30:50.793 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke 调用方法[manageApp]时发生异常 java.lang.IllegalStateException: 启动子级时出错 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:696) at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1783) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:293) at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:814) at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:460) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:408) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:293) at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:814) at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802) at java.management/com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:472) at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1472) at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1310) at java.base/java.security.AccessController.doPrivileged(AccessController.java:712) at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1412) at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360) at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200) at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197) at java.base/java.security.AccessController.doPrivileged(AccessController.java:712) at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196) at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:587) at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828) at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:705) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:704) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: org.apache.catalina.LifecycleException: 无法启动组件[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/flylolo_readcode]] at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726) ... 42 more Caused by: java.lang.NoClassDefFoundError: jakarta/servlet/ServletContainerInitializer at java.base/java.lang.ClassLoader.defineClass1(Native Method) at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012) at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150) at org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2478) at org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:870) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1371) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:467) at org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:226) at org.apache.catalina.startup.WebappServiceLoader.load(WebappServiceLoader.java:197) at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1840) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1298) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:986) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:303) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5135) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ... 43 more Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletContainerInitializer at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215) ... 61 more
通过错误信息中的“Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletContainerInitializer”可以看出是缺少对应的包,网上搜了有类似的错误,少的却不是这个包,后来尝试把Tomcat改为10.0.12(出错时为Tomcat 9),此问题解决,应该是最新的Tomcat中存在此包。
5 GitHub地址
https://github.com/FlyLolo/spring-framework