解決IDEA無法讀取maven鏡像,jar包下載失敗的問題
最近從公司私服下載jar包一直失敗,之前的解決方法是手動下載項目,自己打包來解決的,最近忍無可忍,自己研究了o(???)o.
原因
idea現在的版本要求maven的鏡像必須是https的,而之前的配置的鏡像都是http的,所以鏡像加載失敗了,讀取的是maven默認的倉庫,所以下不下來.
我為什么要手賤升級,明明18年版的也可以用的o(???)o
解決
設置https庫
國內的鏡像基本都有https版本了(公司的就要公司自己配置了),設置一下
<!--阿里倉庫--> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>huaweicloud</id> <mirrorOf>*</mirrorOf> <url>https://mirrors.huaweicloud.com/repository/maven/</url> </mirror>
再到默認的maven設置中為VM添加
-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
在項目中強制導入
公司的項目使用公司的私服,而又沒有配置https的話,就要自己處理一下了.
在項目的pom中添加地址
<repositories> <repository> <id>nexus</id> <name>nexus</name> <url>http://xxxxx/content/groups/public/</url> <!--私服地址--> <releases><enabled>true</enabled> </releases> <snapshots><enabled>true</enabled> </snapshots> </repository> </repositories>
清理settings.xml中的鏡像地址
不知道什么原因,雖然配置了上面的,但是我settings.xml的配置還會影響,必須將里面的自定義的鏡像全部清理掉
此時刷新maven,就能從在pom配置的地址中下載依賴了
建議復制一個maven,里面的鏡像庫清理掉,需要下載http鏡像的時候,就將maven選中這個,就不用專門去清理了
補充知識: 解決 Jackson反序列化 Unexpected token ... , expected VALUE_STRING: need JSON String that contains type id (for subtype of ...)
首先檢查是否是 objectMapper.enableDefaultTyping(); 的受害者。優先考慮刪除該配置。
使用Jackson把數組的json字符串反序列化為List時候報了個JsonMappingException。
java.lang.UnsupportedOperationException: com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: need JSON String that contains type id (for subtype of java.util.List) at [Source: [ ......
找到問題代碼,粗看似乎沒什么問題?
List<MyObject> objectList = objectMapper.readValue(jsonString, new TypeReference<List<MyObject>>() {}); //jsonString是個json對象的數組
注意到異常信息“need JSON String that contains type id (for subtype of java.util.List)”。想了一會兒,好吧,有答案了。
List<MyObject> objectList = objectMapper.readValue(jsonString, new TypeReference<ArrayList<MyObject>>() {}); //jsonString是個json對象的數組
其實,有一種比較老派的反序列化為List的方式...
List<MyObject> objectList = Arrays.asList(objectMapper.readValue(jsonString, MyObject[].class)); //jsonString是個json對象的數組
當對一些較復雜的對象進行反序列化時,例如擁有接口類型成員變量的類。舉個栗子:
@Datapublic class TypeValue { private Integer type; private List<Integer> value;}
有上面這個類,需要把json字符串反序列化成 Map<String, TypeValue> 這樣的對象,怎么做?
可以在TypeValue這個類中使用 @JsonCreator 注解解決。
@Datapublic class TypeValue { private Integer type; private List<Integer> value; @JsonCreator //為Jackson提供構造函數 public TypeValue(@JsonProperty('type') final Integer type, @JsonProperty('value') final int[] value) { this.type= type; this.value = Ints.asList(value); }}
Jackson能夠把[]數組轉換為List。因此可以用以上方法繞過Jackson的限制。
以上使用了guava和lombok。
好了就介紹到這,希望能給大家一個參考,也希望大家多多支持好吧啦網。