1. 程式人生 > >Spring boot開發筆記

Spring boot開發筆記

1.自動掃描配置不需要掃描某個包或者某個類,下面的寫法不會掃描org.xxx.yyy.*下的包以及MyClassToExclude.class

 

@ComponentScan(            
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.xxx.yyy.*"),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = MyClassToExclude.class) })


2. Multipart上傳檔案,過一段時間以後將會出現org.springframework.web.multipart.MultipartException,解決辦法在大神的部落格中,增加-Djava.io.tmpdir啟動引數,或者修改程式new MultipartConfigFactory().setLocation("/app/pttms/tmp"),我採用的是在配置檔案中增加server.tomcat.basedir=${user.home}/deployer/tomcat

 

3.彈出瀏覽器自帶的使用者名稱和密碼框, 輸入使用者名稱和密碼以後每次請求在Header中都會自動帶上Authorization: Basic Base64.encode("username:password")

	public static void ret401(HttpServletRequest request, HttpServletResponse response){
		String serverName = request.getServerName();
		response.setStatus(401);
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
		response.setHeader("WWW-authenticate", "Basic Realm=\"" + serverName + "\"");
	}

4.RestTemplate的坑

RestTemplate會對url會自動進行編碼,如果url的引數已經編碼過就會出現重複編碼, 並且帶有特殊符號的引數自動編碼的結果和URLEncoder.encode編碼的結果並不是完全一致

 解決辦法如下: 如果url的引數已經Encode過了則不再重新編碼, UricomponentsBuilder.fromHttpUrl(url).build(true).toUri()或者new URI(url)

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
// url的引數已經編碼過了,所以不需要再次編碼
ResponseEntity<String> responseEntity = restTemplate.getForEntity(builder.build(true).toUri(), String.class);

或者

ResponseEntity<String> responseEntity = restTemplate.getForEntity(new URI(url), String.class);