關於學習並使用springboot遇到的一些問題

問題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”即可,不能有駝峯命名。只能小寫。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章