JSON 错误中的意外令牌但我正在使用 'produces = "text/plain"'?
Unexpected Token in JSON error but I'm using 'produces = "text/plain"'?
我在网络应用程序中调用以下函数时遇到错误:
getUML() {
const testName = this.formdata.get('testName');
this.disableDeployCTA = true;
const params:string = testName + "/txt";
const url = `http://localhost:8081/bruml/export/${params}`
this.httpService.generateUML(params).subscribe((response: any) => {
this.umlText = response.data;
});
}
请求正在通过 Spring 引导应用程序,使用此方法:
@GetMapping(value = "/export/{testName}/txt", produces = "text/plain")
public ResponseEntity<String> exportTxt(@PathVariable("testName") String name) {
UMLClient myClient = new UMLClient();
String txtUML = "";
byte[] toExport = null;
try {
String fileName = ".\tempFiles\" + name;
myClient.export(name, fileName + ".txt", true);
File txtTemp = new File(fileName + ".txt");
File file = new File(fileName + ".svg");
Path filePath = Paths.get(fileName + ".txt");
Charset charset = StandardCharsets.UTF_8;
txtUML = new String(Files.readAllBytes(filePath));
if(file.exists() && txtTemp.exists()) {
file.delete();
txtTemp.delete();
}
}
catch (Exception e){
e.printStackTrace();
}
return new ResponseEntity<>(txtUML, HttpStatus.OK);
}
生成UML方法如下:
generateUML(params) {
return this.httpClient.get('http://localhost:8081/bruml/export/'+ params);
}
我收到一个错误“JSON 中的意外标记 @”。但我不希望它解析为 JSON——我将 produces 属性设置为纯文本。我该如何解决这个问题?
编辑**
我在控制台中收到此错误
这些是我在开发工具中的网络选项卡中关于请求和响应的详细信息:
@anoncomp 你能 post 来自浏览器网络选项卡的请求和响应吗?如果你得到 200
响应,那么我们可以看到你从后端得到的到底是什么。如果您得到正确的响应,那么问题将出在这一行:
this.umlText = response.data;
因为您期望响应是一个对象。
您可能需要解析响应。
编辑:您在使用 Angular 吗?如果是这样,您是否可以尝试在 get
请求中发送选项,同时使用 responseType
作为 text
?供参考:Angular Docs。默认情况下 Angular 将响应视为 JSON:
来自 angular 文档:
The get() call needs the following options: {observe: 'body', responseType: 'json'}
.
这些是这些选项的默认值,因此以下示例不传递选项对象。
我在网络应用程序中调用以下函数时遇到错误:
getUML() {
const testName = this.formdata.get('testName');
this.disableDeployCTA = true;
const params:string = testName + "/txt";
const url = `http://localhost:8081/bruml/export/${params}`
this.httpService.generateUML(params).subscribe((response: any) => {
this.umlText = response.data;
});
}
请求正在通过 Spring 引导应用程序,使用此方法:
@GetMapping(value = "/export/{testName}/txt", produces = "text/plain")
public ResponseEntity<String> exportTxt(@PathVariable("testName") String name) {
UMLClient myClient = new UMLClient();
String txtUML = "";
byte[] toExport = null;
try {
String fileName = ".\tempFiles\" + name;
myClient.export(name, fileName + ".txt", true);
File txtTemp = new File(fileName + ".txt");
File file = new File(fileName + ".svg");
Path filePath = Paths.get(fileName + ".txt");
Charset charset = StandardCharsets.UTF_8;
txtUML = new String(Files.readAllBytes(filePath));
if(file.exists() && txtTemp.exists()) {
file.delete();
txtTemp.delete();
}
}
catch (Exception e){
e.printStackTrace();
}
return new ResponseEntity<>(txtUML, HttpStatus.OK);
}
生成UML方法如下:
generateUML(params) {
return this.httpClient.get('http://localhost:8081/bruml/export/'+ params);
}
我收到一个错误“JSON 中的意外标记 @”。但我不希望它解析为 JSON——我将 produces 属性设置为纯文本。我该如何解决这个问题?
编辑**
我在控制台中收到此错误
这些是我在开发工具中的网络选项卡中关于请求和响应的详细信息:
@anoncomp 你能 post 来自浏览器网络选项卡的请求和响应吗?如果你得到 200
响应,那么我们可以看到你从后端得到的到底是什么。如果您得到正确的响应,那么问题将出在这一行:
this.umlText = response.data;
因为您期望响应是一个对象。
您可能需要解析响应。
编辑:您在使用 Angular 吗?如果是这样,您是否可以尝试在 get
请求中发送选项,同时使用 responseType
作为 text
?供参考:Angular Docs。默认情况下 Angular 将响应视为 JSON:
来自 angular 文档:
The get() call needs the following options:
{observe: 'body', responseType: 'json'}
.
这些是这些选项的默认值,因此以下示例不传递选项对象。