application.yaml
中配置沒有生效問題解決若是配置文件確認沒有錯誤可是沒有生效首先是要到編譯目錄去查看是否被編譯過去了,若是沒有,請先將項目clean在重啓 可是idea啓動項目時也會先build,又有可能配置文件沒有被編譯過去,真實坑爹! 另外,yaml文件中的那些坑: (1)冒號:後面必須有空格,下級屬性縮進一格(只支持空格不支持製表符tab) (2)保證不能有重複的一級節點。 (3)若是參數是以空格開始或結束的字符串,應使用單引號把他包進來。若是一個字符串參數包含特殊字符,也要用單引號包起來。 若是字符串中自己包含單引號,則須要用‘’進行轉義;若是字符串開頭或結尾包含空格,則須要用單引號將整個字符串包裹html
SpringBoot 2.0.0.RELEASE版本後更新java
server:
servlet:
context-path: /example
複製代碼
server.servlet.context-path=/example
複製代碼
背景:spring-boot項目,打包成可執行jar,項目內有兩個帶有main方法的類而且都使用了@SpringBootApplication註解(或者另外一種情形:你有兩個main方法而且所在類都沒有使用@SpringBootApplication註解) web
<plugins>
<plugin>
<!--該插件主要用途:構建可執行的JAR -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration><!-- 指定該Main Class爲全局的惟一入口 -->
<mainClass>com.sbcm.UserApplication</mainClass>
<layout>ZIP</layout>
<outputDirectory>
${package.base.url}
</outputDirectory>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<!--能夠把依賴的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
複製代碼
在工做中,不少狀況下咱們打包是不想執行測試用例的,多是測試用例不完事,或是測試用例會影響數據庫數據.spring
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
複製代碼
因此在用到數據庫的時候記得將他改成 @SpringBootApplication ,sql
不然會報錯:以下chrome
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxController': Unsatisfied dependency expressed through field 'xxMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxMapper' defined in file [D:\workspacesidea\pear\target\classes\com\wqq\mapper\xxMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required數據庫
背景: 項目兩個獨立模塊user和task,兩個獨立的服務提供模塊,如今只啓動了user模塊,想在瀏覽器訪問task模塊的服務。express
方法: 1,把task也啓動,這樣最簡單,不過有多個其餘模塊也要訪問的話只能都啓動,明顯不太方便。apache
2,把task加到user的依賴裏面去,就是在user的pom裏面添加task模塊,還須要在user的掃描的註解@ComponentScan裏面擴大範圍,讓他能掃描到task的註解,否則task的服務也找不到,不過這樣會致使強耦合,由於user和task是兩個獨立的服務模塊json
3,使用Spring cloud,把user和task註冊到服務中心,互相經過serverName調用。(推薦)
一、問題:
自己spingboot項目是用@RestController註解,返回結果也是json格式,可是結合springcloud的eureka構建微服務以後,不管是消費者仍是提供者,均返回的xml格式
複製代碼
二、分析
今天正好遇到了這個問題,查閱了不少東西大體弄明白了。引入了jackson-dataformat-xml
這個依賴,它是提供了jackson
將實體類轉化爲xml相關的做用。而自己jackson
是能夠將實體類轉化爲json的,因此這樣Jackson是能夠將實體類轉化爲兩種類型的數據,而具體要轉化爲哪種數據,是要看http請求裏面的accept頭信息的,個人瀏覽器chrome的accept是 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
,而後服務器會根據accept來決定是返回xml仍是json,因爲瀏覽器accept只有最後的*/*是匹配 application/json的,而application/xml在/前面,優先級比json高,因此用瀏覽器直接調用是會優先返回xml格式的。
三、解決方案有兩種:
1.本身調用接口的時候修改accept信息,改成application/json (postman之類的工具) 提供者與消費者的方法上或者所屬類上添加 produces=「application/json」,
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-xml-provider</artifactId>
</dependency>
複製代碼
而後就可使用後綴來調用相關的接口獲取對應格式的數據了。好比我有個url localhost/get/user 返回一個用戶數據添加了上面的依賴後,若是想獲取xml格式的,就使用localhost/get/user.xml來調用接口;若是想獲取json格式就要用localhost/get/user.json來調用接口它的原理是服務器根據後綴主動修改了accept信息