使用DateTimeFormatterBuilder建立符合原始字串的Formatter,用來將原始字串parse成相應LocalDateTime物件
再將LocalDateTime物件透過目標格式的Formatter格式化成我們所想要的日期時間格式字串
DateTimeFormatter jsonDateTimeFormatter = new DateTimeFormatterBuilder()
.appendLiteral("/Date(")
.appendValue(ChronoField.INSTANT_SECONDS)
.appendValue(ChronoField.MILLI_OF_SECOND,3)
.appendOffset("+HHmm","+0000")
.appendLiteral(")/")
.toFormatter();
DateTimeFormatter targetDateTimeFormatter = DateTiemFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
@JsonProperty("startDate")
private String startDate;
public void setStartDate(String startDate){
if(startDate.matches("^\\/Date\\(\\d{13,13}\\+\\d{4,4}\\)\\/$")){
LocalDatetime dateTime = LocalDateTime.parse(startDate,jsonDateTimeFormatter);
startDate = dateTime.format(targetDateTimeFormatter);
}
this.startDate = startDate;
}
]]>使用DateTimeFormatterBuilder建立符合原始字串的Formatter,用來將原始字串parse成相應LocalDateTime物件
再將LocalDateTime物件透過目標格式的Formatter格式化成我們所想要的日期時間格式字串
DateTimeFormatter jsonDateTimeFormatter = new DateTimeFormatterBuilder()
.appendLiteral("/Date(")
.appendValue(ChronoField.INSTANT_SECONDS)
.appendValue(ChronoField.MILLI_OF_SECOND,3)
.appendOffset("+HHmm","+0000")
.appendLiteral(")/")
.toFormatter();
DateTimeFormatter targetDateTimeFormatter = DateTiemFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
@JsonProperty("startDate")
private String startDate;
public void setStartDate(String startDate){
if(startDate.matches("^\\/Date\\(\\d{13,13}\\+\\d{4,4}\\)\\/$")){
LocalDatetime dateTime = LocalDateTime.parse(startDate,jsonDateTimeFormatter);
startDate = dateTime.format(targetDateTimeFormatter);
}
this.startDate = startDate;
}
]]>
最後針對Tomcat兩處設定做了調整,有成功解決亂碼問題:
1.調整Tomcat conf資料夾下的server.xml 在Connector標籤中加入 URIEncoding = "UTF-8"
<Connector
port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8" #新增
connectionTimeout="20000"
redirectPort="8443" />
2.調整Tomcat bin資料夾下的catalina.bat 在最前面 @echo off 後面插入一行
@echo off
set JAVA_OPTS = -Dfile.encoding=UTF-8 #新增
]]>最後針對Tomcat兩處設定做了調整,有成功解決亂碼問題:
1.調整Tomcat conf資料夾下的server.xml 在Connector標籤中加入 URIEncoding = "UTF-8"
<Connector
port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8" #新增
connectionTimeout="20000"
redirectPort="8443" />
2.調整Tomcat bin資料夾下的catalina.bat 在最前面 @echo off 後面插入一行
@echo off
set JAVA_OPTS = -Dfile.encoding=UTF-8 #新增
]]>
解決方法:經由request的InputStream讀取資料
前端使用fetch 進行post 請求 程式碼如下:
```
let obj = {
id:"userid",
name:"username"
}
fetch('/user',{
method:'post',
headers:{'Content-Type':'application/json'},
body:JSON.stringify(obj)
}).then(response=>{
return response.json();
}).then(datas=>{
console.log(datas);
}).catch(err=>console.log(err))
```
Servlet 程式碼如下: (使用到套件 org.json 中的JSONObject類 利用讀取到的的json格式String建立JSONObject物件)
```
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
String json = "";
if (br != null) {
json = br.readLine();
}
JSONObject obj = new JSONObject(json);
//後續即可使用obj物件的方法取得對應key的值
```
參考文章說到這個狀況是由於Tomcat對於Content-Type是multipart/form-data及application/x-www-form-urlencoded的POST請求進行了特殊處理,所以可以使用getParameter讀取,而對於其他類型的POST因為資料可能有不同的格式而沒有進行特殊處理,以致於getParameter方法無法如我們原先預期的讀取到資料。
參考文章:https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/301708/
]]>解決方法:經由request的InputStream讀取資料
前端使用fetch 進行post 請求 程式碼如下:
```
let obj = {
id:"userid",
name:"username"
}
fetch('/user',{
method:'post',
headers:{'Content-Type':'application/json'},
body:JSON.stringify(obj)
}).then(response=>{
return response.json();
}).then(datas=>{
console.log(datas);
}).catch(err=>console.log(err))
```
Servlet 程式碼如下: (使用到套件 org.json 中的JSONObject類 利用讀取到的的json格式String建立JSONObject物件)
```
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
String json = "";
if (br != null) {
json = br.readLine();
}
JSONObject obj = new JSONObject(json);
//後續即可使用obj物件的方法取得對應key的值
```
參考文章說到這個狀況是由於Tomcat對於Content-Type是multipart/form-data及application/x-www-form-urlencoded的POST請求進行了特殊處理,所以可以使用getParameter讀取,而對於其他類型的POST因為資料可能有不同的格式而沒有進行特殊處理,以致於getParameter方法無法如我們原先預期的讀取到資料。
參考文章:https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/301708/
]]>年後又開啟每天去圖書館報到的日子,趕著在3月開課前預習了出版年代有點久遠的 深入淺出 Java 第二版,中譯版的內容錯誤的地方實在有點多,不過書上有很多過去讀者留下來的訂正註記,真的是很可愛。
Java班開課後,步調反而慢了下來,課程從基礎開始教進度緩慢,除了Java之外還有資料庫、企業系統規劃跟網頁前端的課程,實體課程的缺點大概就是步調沒有辦法依照自己想要的調整,時間相對上比較不自由,下課之後的時間不多,整理上課的筆記、梳理之前學到的並做一些稱不上是專案的小應用,一邊思考後續要做什麼樣的專案,時間很零碎實在讓人有點痛苦。
接下來週末可以專注地去圖書館待兩天,預計要練習將之前做的小功能用React改寫,然後要來思考一個迷你專案用flask及react實作,上架到heroku上。
加油!
]]>年後又開啟每天去圖書館報到的日子,趕著在3月開課前預習了出版年代有點久遠的 深入淺出 Java 第二版,中譯版的內容錯誤的地方實在有點多,不過書上有很多過去讀者留下來的訂正註記,真的是很可愛。
Java班開課後,步調反而慢了下來,課程從基礎開始教進度緩慢,除了Java之外還有資料庫、企業系統規劃跟網頁前端的課程,實體課程的缺點大概就是步調沒有辦法依照自己想要的調整,時間相對上比較不自由,下課之後的時間不多,整理上課的筆記、梳理之前學到的並做一些稱不上是專案的小應用,一邊思考後續要做什麼樣的專案,時間很零碎實在讓人有點痛苦。
接下來週末可以專注地去圖書館待兩天,預計要練習將之前做的小功能用React改寫,然後要來思考一個迷你專案用flask及react實作,上架到heroku上。
加油!
]]>