常見(jiàn)錯(cuò)誤

  1. Test is not an annotation type
    描述:在使用junit進(jìn)行單元測(cè)試時(shí),在方法前聲明@Test時(shí)顯示Test is not an annotation type錯(cuò)誤
    原因:此類(lèi)或其他類(lèi)中有名為T(mén)est的類(lèi),加載@Test時(shí)不能加載到j(luò)unit中的test單元,而是調(diào)用了相同的類(lèi)名
    解決辦法:查看名為T(mén)est的類(lèi),將其改名不與junit中的Test沖突即可

  2. 配置文件中的jdbc.username,如果寫(xiě)成username,可能會(huì)與系統(tǒng)環(huán)境中的username變量沖突,所以到時(shí)候真正連接數(shù)據(jù)庫(kù)的時(shí)候,用戶(hù)名就被替換成系統(tǒng)中的用戶(hù)名(有得可能是administrator),那肯定是連接不成功的

  3. Could not find resource ...UserMapper.xml
    原因:mybatis-config中加載mappers時(shí)路徑出錯(cuò)
    解決辦法:修改路徑或批量加載

  4. unsupported major.minor version 51.0
    原因:jdk版本太低
    解決辦法:換成jdk1.8即可

  5. 注意:使用@Override注解時(shí),jdk和maven版本必須都高于1.5版本,否則無(wú)法使用此注解

  6. java.lang.ClassNotFoundException:${jdbc.driver}
    原因:<context:property-placeholder location="classpath*:jdbc.properties"/>未載入屬性,因?yàn)樵趕pring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 進(jìn)行自動(dòng)掃描的時(shí)候,設(shè)置了sqlSessionFactory 的話(huà),可能會(huì)導(dǎo)致PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}這樣之類(lèi)的表達(dá)式,將無(wú)法獲取到properties文件里的內(nèi)容。 導(dǎo)致這一原因是因?yàn)?,MapperScannerConigurer實(shí)際是在解析加載bean定義階段的,這個(gè)時(shí)候要是設(shè)置sqlSessionFactory的話(huà),會(huì)導(dǎo)致提前初始化一些類(lèi),這個(gè)時(shí)候,PropertyPlaceholderConfigurer還沒(méi)來(lái)得及替換定義中的變量,導(dǎo)致把表達(dá)式當(dāng)作字符串復(fù)制了。 但如果不設(shè)置sqlSessionFactory 屬性的話(huà),就必須要保證sessionFactory在spring中名稱(chēng)一定要是sqlSessionFactory ,否則就無(wú)法自動(dòng)注入。又或者直接定義 MapperFactoryBean ,再或者放棄自動(dòng)代理接口方式。
    解決辦法:將<property name="sqlSessionFactory" ref="sqlSessionFactory" />換成
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

  7. Several ports (8005, 8080, 8009) required by Tomcat v6.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).
    shutdown.bat

  8. 無(wú)法加載,無(wú)法讀取配置文件時(shí),有可能是系統(tǒng)緩存未清除,可以嘗試clean,重啟

  9. jsp頁(yè)面無(wú)法使用外部靜態(tài)資源,使用不了外部樣式
    解決辦法:
    <mvc:resources location="/css/" mapping="/css/*"/>

  10. org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.asiainfo.pojo.User] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
    {@org.springframework.beans.factory.annotation.Autowired(required=true)}

  11. 查詢(xún)數(shù)據(jù)庫(kù)信息時(shí)顯示nullpointerexception:
    原因:可能是resultType和查詢(xún)結(jié)果不對(duì)應(yīng)
    解決辦法:設(shè)置別名

  12. 打開(kāi)Eclipse提示The default workspace “xxxx” is in use or cannot be created Please choose a different one
    原因:出現(xiàn)這種情況一般是workspace的配置文件中出現(xiàn)了.lock文件(workspace/.metadata/.lock),鎖定了workspace。把.lock文件刪除即可。
    如果該文件不能刪除,可能是因?yàn)閖avaw.exe進(jìn)程未結(jié)束,結(jié)束該進(jìn)程及eclipse.exe進(jìn)程即可刪除。

正常情況下,如果你打開(kāi)了一個(gè)workspace,在想打開(kāi)另一個(gè)workspace也會(huì)出現(xiàn)上面的提示

  1. 錯(cuò)誤: 找不到或無(wú)法加載主類(lèi):
    1)可能是找不到.jar包
    解決辦法:build path,導(dǎo)入或直接刪除包即可
    2)jar包沖突
    解決辦法:刪除pom.xml新添加的依賴(lài),或者新添加的jar包
    3)project->clean重新構(gòu)建class

  2. UnsupportedClassVersionError
    Unsupported major.minor version 51.0
    jre版本不一致問(wèn)題
    解決辦法:
    jre右鍵->properties->選擇對(duì)應(yīng)jdk版本即可

  3. JSP中使用include指令出現(xiàn)“Duplicate local variable basePath”錯(cuò)誤提示后的發(fā)現(xiàn)
    解決辦法:將不同頁(yè)面中basePath改為不同變量名即可

  4. springboot集成redis緩存時(shí),引入redis依賴(lài)就報(bào)錯(cuò),項(xiàng)目啟動(dòng)不了
    原因:依賴(lài)沖突,倉(cāng)庫(kù)中已經(jīng)有了spring-data-redis依賴(lài)(非自己引入,原本就有),再引入spring-boot-starter-redis依賴(lài)就會(huì)與原來(lái)的沖突,導(dǎo)致項(xiàng)目報(bào)錯(cuò),出現(xiàn)找不到或無(wú)法加載主類(lèi)、找不到對(duì)應(yīng)類(lèi)、class字節(jié)碼文件丟失等原因
    解決辦法:找到倉(cāng)庫(kù)原redis依賴(lài),直接刪除,引入spring-boot-starter-redis依賴(lài)即可

  5. springboot集成redis時(shí)報(bào)錯(cuò):
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] found for dependency
    原因:沒(méi)有JedisConnectionFactory 工廠(chǎng)bean
    解決辦法:config配置類(lèi)中加入:

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }
  1. springboot集成redis從數(shù)據(jù)庫(kù)中讀取不到數(shù)據(jù),顯示空指針錯(cuò)誤
    原因:可能是先已經(jīng)緩存了對(duì)應(yīng)為空的數(shù)據(jù),然后查詢(xún)數(shù)據(jù)是直接從緩存中讀取,所以讀取到null
    解決辦法:刪除緩存數(shù)據(jù),或更新緩存數(shù)據(jù)即可

  2. spring-data-jpa使用報(bào)錯(cuò):user is not mapped
    當(dāng)jpa提供的方法不足以滿(mǎn)足業(yè)務(wù)開(kāi)發(fā)需求時(shí),我們可以自定義查詢(xún)方式,其中自定義查詢(xún)又有兩種方式:

1)@NamedQuery
                @Query(value="select * from t_user u where u.username=?1",nativeQuery=true)  
                public User findByusername(String username);
        2)@Query
                @Query("select u from User u where u.password=:password")  
                public User findByPassword(@Param("password") String password);

注意:第二種查詢(xún)方式中,User是對(duì)應(yīng)的實(shí)體類(lèi)名,而非數(shù)據(jù)庫(kù)表名,如果使用user,就會(huì)報(bào)錯(cuò):user is not mapper
解決辦法:將user修改為實(shí)體類(lèi)名User即可

  1. Name for parameter binding must not be null or empty
    解決辦法:加上@Param("")注解, List<ShopOrder> getShopOrder(@Param("id") long id);

  2. Expected CSRF token not found. Has your session expired 403
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />

  3. springboot+springsecurity+thymeleaf創(chuàng)建項(xiàng)目時(shí),訪(fǎng)問(wèn)控制器方法卻定位到static靜態(tài)資源,而且,所有樣式都失效
    原因:springsecurity安全攔截了靜態(tài)資源
    解決辦法:WebSecurityConfig中,重寫(xiě)方法configure

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**", "/images/**", "/js/**");
    }
  1. NoSuchBeanDefinitionException: No qualifying bean of type [com.sj.cms.dao.UserDao] found for dependency
    原因:springboot整合mybatis時(shí)添加@Mapper

  2. springboot整合mybatis: Invalid bound statement (not found)
    原因:mybatis配置路徑錯(cuò)誤
    解決辦法:mybatis:
    mapper-locations: classpath:com/sj/vip/mapper/*.xml type-aliases-package: com.sj.vip.entity

  3. springboot整合mybatis 找不到dao或impl
    解決辦法:dao中加注解@Mapper,如:

@Mapper
public interface UserDao {
    User findUserByUserName(String username);
}
  1. springboot啟動(dòng)Address already in use: bind
    原因:端口被占用
    解決辦法:?jiǎn)?dòng)任務(wù)管理器,結(jié)束javaw.exe進(jìn)程

  2. 第三方應(yīng)用獲取QQ授權(quán)時(shí)回調(diào)顯示錯(cuò)誤:{"ret":100031,"msg":"app has no privilege to use this api"} {"ret":100008,"msg":"client request's app is not existed"}
    應(yīng)該是開(kāi)發(fā)者申請(qǐng)審核未通過(guò)的問(wèn)題,待定...

  3. ClassNotFoundException: redis.clients.jedis.JedisPoolConfig
    原因:可能是版本問(wèn)題,重啟eclipse

  4. ajax請(qǐng)求時(shí),點(diǎn)擊按鈕沒(méi)反應(yīng),原因可能是:
    data:{ password: password; },
    password結(jié)尾應(yīng)該是逗號(hào),而非分號(hào)
    修改為,即可

  5. 引入net.sf.json maven依賴(lài)時(shí),報(bào)錯(cuò):Missing artifact net.sf.json-lib:json-lib:jar:2.4

<dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
</dependency>
        解決辦法:需要加入:<classifier>jdk15</classifier>
<dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
</dependency>

搞定

  1. 利用JSONObject將json字符串轉(zhuǎn)為json對(duì)象JsonObject時(shí)出現(xiàn):Expected a ',' or '}' at character 37 of ...
    原因:json字符串中必須為雙引號(hào),不能為單引號(hào),否則解析異常,如:
        String str = "{'ret':100008,'msg':'client request's app is not existed'}";報(bào)錯(cuò)!
        String str = "{\"ret\":100008,\"msg\":\"client request's app is not existed\"}";正確!
  1. 錯(cuò)誤描述:ajax回調(diào)方法返回狀態(tài):jqxhr.status總是0,而且明明使用post方式提交數(shù)據(jù)卻變成get方式
    原因:在<form></form>表單中,<input type="submit"></input>已經(jīng)定義了input類(lèi)型為提交submit,那么此時(shí)如果使用ajax異步提交,就會(huì)產(chǎn)生沖突,這也就是為什么明明定義$.post方式卻url地址欄傳遞了參數(shù),簡(jiǎn)言之,當(dāng)你點(diǎn)擊按鈕時(shí),首先會(huì)因?yàn)閟ubmit直接提交form表單數(shù)據(jù),然后click方法中的ajax post方法就會(huì)二次提交數(shù)據(jù),這樣就導(dǎo)致了status=0的問(wèn)題
    解決辦法:直接去掉type="submit"即可

靈感來(lái)自:https://stackoverflow.com/questions/14313283/jquery-ajax-jqxhr-status-is-always-0

It is because you are not cancelling the click event of the submit button. You need to do it for both cases, not just the part in the if. Move that line before the if, the code will magically not be zero anymore.

題外話(huà):stackoverflow確實(shí)是個(gè)很不錯(cuò)的網(wǎng)站,大家有興趣可以多逛逛,雖然全英文,但是搞it的經(jīng)常接觸就會(huì)的多了,看的懂了,學(xué)到技術(shù)的同時(shí)還能練一下English

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容