[問題解決】JSONパースエラー。不正な非引用符合文字 ((CTRL-CHAR, code 13)): を使ってエスケープする必要があります。
2022-02-07 09:16:19
同社は最近、私が行うには画像を受け入れるために、外部インタフェースを持っていた、私はそれがとても簡単だと思った(と怠惰なことができる)、画像を参照してくださいにドキュメントを開くと、何も、それをバイトストリームにbase64形式ではありません。同じ操作の後、受け入れパラメータをテストし、この事からクラッシュします。私はそれが正しい形式であることを確認した最初のものでした。
まず、なぜ例外が発生したのかを分析します
-
さて-。- Dounianの翻訳によると、jsonのパースの問題で、送信された文字の中に転送する必要がある文字があり、そうでないと受け付けないからだと分かっています。
つまり、問題の80%は、エスケープされた文字でパラメータを送信し、バックエンドがそれを受け入れるためにjsonを使用していることに起因し、プログラムが
**( [ { / ^ - $ ¦ } ] ) ? * + . ** エスケープしないと解析されません。
問題解決
Then someone must ask the owner since you write a blog post is to solve the problem, I since this search will certainly be the need to lose these characters ah.
Not in a hurry. The owner posted his solution.
In fact, the idea is very simple
nested exception is com.fasterxml.jackson.databind.JsonMappingException
JsonMappingException is not to accept the escaped parameters. Then we take the request to accept it is not good.
We're not in a hurry, I'll post the code.
まず、コントローラ層がパラメータを受け取るとき
を削除するために
produces = "application/json;charset=UTF-8"
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@RestController
@Slf4j
public class BiZiTeOtherController {
@RequestMapping(value = "/test/UploadImage",method = RequestMethod.POST)
public String uploadImageBiZiTe(HttpServletRequest request){
//GetRequestJsonUtils this tool class in the following oh
String requestPostStr = GetRequestJsonUtils.getRequestPostStr(request);
System.out.println(requestPostStr);
// Anyway, the one the owner uses can be converted to the object I need properly
// <dependency>
// <groupId>com.alibaba</groupId>
// <artifactId>fastjson</artifactId>
// <version>1.2.47</version>
// </dependency>
UploadImg uploadImg = JSONObject.parseObject(requestPostStr, UploadImg.class);
log.info(uploadImg.toString());
return "Success";
}
}
class UploadImg{
//picture class
private String picClass;
//image associated primary key
private String refId;
//base64 image string
private String pic;
@Override
public String toString() {
return "uUploadImg{" +
"picClass='" + picClass + '\'' +
", refId='" + refId + '\'' +
", pic='" + pic + '\'' +
'}';
}
public String getPicClass() {
return picClass;
}
public void setPicClass(String picClass) {
this.picClass = picClass;
}
public String getRefId() {
return refId;
}
public void setRefId(String refId) {
this.refId = refId;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
/**
* Convert reqeest tool class
**/
public class GetRequestJsonUtils {
public static JSONObject getRequestJsonObject(HttpServletRequest request) throws IOException {
String json = getRequestJsonString(request);
return JSONObject.parseObject(json);
}
/****
* Get the content of the json string in the request
*
* @param request
* @return : <code>byte[]</code>
* @throws IOException
*/
public static String getRequestJsonString(HttpServletRequest request)
throws IOException {
String submitMehtod = request.getMethod();
// GET
if (submitMehtod.equals("GET")) {
return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\& quot;");
// POST
} else {
return getRequestPostStr(request);
}
}
/* @param request
* @return
* @throws IOException
*/
public static byte[] getRequestPostBytes(HttpServletRequest request)
throws IOException {
int contentLength = request.getContentLength();
if(contentLength<0){
return null;
}
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength;)
{
int readlen = request.getInputStream().read(buffer, i,
contentLength - i);
if (readlen == -1) {
break;
}
i += readlen;
}
return buffer;
}
/**
* Description:Get the content of the post request
* <pre>
* Example
* </pre>
* @param request
* @return
* @throws IOException
*/
public static String getRequestPostStr(HttpServletRequest request) {
try{
byte buffer[] = getRequestPostBytes(request);
String charEncoding = request.getCharacterEncoding();
if (charEncoding == null) {
charEncoding = "UTF-8";
}
return new String(buffer, charEncoding);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
It is said that the likes are handsome men.
No, there are beautiful women.
These escaped characters in the ** parameter are not required to be accepted. You can restrict the user to enter these characters by the frontend, and the backend will not have to consider whether to turn them over or not. The **
関連
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
ハートビート・エフェクトのためのHTML+CSS
-
HTML ホテル フォームによるフィルタリング
-
HTML+cssのボックスモデル例(円、半円など)「border-radius」使いやすい
-
HTMLテーブルのテーブル分割とマージ(colspan, rowspan)
-
ランダム・ネームドロッパーを実装するためのhtmlサンプルコード
-
Html階層型ボックスシャドウ効果サンプルコード
-
QQの一時的なダイアログボックスをポップアップし、友人を追加せずにオンラインで話す効果を達成する方法
-
sublime / vscodeショートカットHTMLコード生成の実装
-
HTMLページを縮小した後にスクロールバーを表示するサンプルコード
-
html のリストボックス、テキストフィールド、ファイルフィールドのコード例