问题1:自动装配@Autowried 失败
详情:
Service 注入始终为空
@Autowired
public WXuserInfoService wxUserInfoService ; //wxUserInfoService 始终为空
注意一:是否在spring容器的管理里面,如果没有请在类上加@Component
@Component
public class ControllerService {
如果加上@Component 注解启动报错,看一下你的实体类是否是静态的,去掉静态
例如:
public WXuserInfoService wxUserInfoService ;
public static void coreService(HttpServletRequest request, HttpServletResponse response){
}
//这里使用了静态,应该去掉
注意二:Service是否使用@Service 注解,没有请加上,加上如果没有效果,
请使用@Resource(name="WXuserInfoService")指定service并去掉@Autowired注解 ,使用@Resource注解时service层需要指定service接口,直接copy接口名称 例如@Service("WXuserInfoService")
问题2:如何打包项目,并把配置文件等放在jar包外部?
第一步:添加maven 配置:pom.xml文件下面build里面添加:
<!--引用jar,打包时将配置文件外放-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix></classpathPrefix>
<mainClass>com.server.ServerApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- The configuration of the plugin -->
<configuration>
<descriptors>
<!-- 指定你package.xml的位置 -->
<descriptor>src/main/resources/config/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
maven-jar-plugin:负责将应用程序打包成可执行的jar文件
maven-assembly-plugin:负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署
打包的时候一定要指定启动类:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.dgj.weixin.WeixindevApplication</mainClass><!--你的启动类-->
</configuration>
</plugin>
不然在运行的时候会出现找不到启动加载类的错误!
第二步:在src\main\resources下面创建config文件夹,并在下面新建一个assembly.xml文件(这个名字可以自己改只要和pom.xml的指定一致就行)
assembly.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<!--<include>*.sql</include>-->
<include>*.bat</include>
<include>*.md</include>
<!--<include>README*</include>-->
<!--<include>LICENSE*</include>-->
<!--<include>NOTICE*</include>-->
<!--<include>build.info</include>-->
</includes>
</fileSet>
<!-- 我这里用的是yml的配置,如果用properties配置 直接恢复并注释yml的配置即可 -->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>config/*.xml</include>
<!-- <include>config/*.properties</include>-->
<include>config/*.yml</include>
<!-- <include>*.properties</include>-->
<include>*.yml</include>
</includes>
</fileSet>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
以上配置完成了;我这里是使用的IDEA ,所以直接在右边的Maven Project 下面的你的项目下面找到Lifecycle
先clean清理一下,然后install。
在target下面找到打包好的jar,复制粘贴到你的服务器上或者本地执行;
执行命令的时候输入下面的命令:
java -jar -Dspring.config.location=application.yml weixindev-0.0.1-SNAPSHOT.jar
-Dspring.config.location是用来读取你外置application.yml的配置信息;
然后项目启动。
问题三:返回json对象时,出现
"Could not write JSON: Object is null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Object is null (through reference chain: net.sf.json.JSONObject[\"data\"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject[\"_score\"]->net.sf.json.JSONNull[\"empty\"])
我遇到的错误其实就是上面提示的Object is null,
原因就是我的jsonObject对象内的有一个对象的值为null。
如果你的返回值中不存在null,不会报错,一旦有null字段,则无法插入到json中,并报上述错误。
解决方法:判断需要封装的字符串中是否存在对象"object":null ,如果有将null替换成 ""双引号行了。
问题四:
我的springboot 版本 2.1.0
springboot1.5以下版本貌似不存在这种问题:
@ConfigurationProperties(prefix = “Excelpath”)
注解读取application.yml文件中的系统参数时,不能有大写字母改为“excelpath”即可,不能有驼峰命名。只能小写。