json错误中出现意外标记,但我使用的是“products=”text/plain“?

2nbm6dog  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(309)

我在web应用程序中调用以下函数时出错:

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); 
}

生成方法如下:

generateUML(params) {
    return this.httpClient.get('http://localhost:8081/bruml/export/'+ params);
  }

我得到了一个错误,“json中的意外标记@”。但我不想将其解析为json——我将products属性设置为纯文本。我怎样才能解决这个问题?
编辑**
我在控制台中得到这个错误

以下是dev tools中“我的网络”选项卡中有关请求和响应的详细信息:



fcy6dtqo

fcy6dtqo1#

@anoncomp您是否可以从浏览器的“网络”选项卡发布请求和响应,以及 200 然后我们就可以看到你从后端得到了什么。如果您得到了正确的响应,那么问题将出现在这一行: this.umlText = response.data; 因为您希望响应是一个对象。您可能需要解析响应。
编辑:你用的是Angular 吗?如果是这样的话,你能试着用一个 get 请求 responseType 作为 text 也供参考:Angular 文档。默认的Angular 考虑响应为JSON:
从Angular 文档:
get()调用需要以下选项: {observe: 'body', responseType: 'json'} .
这些是这些选项的默认值,因此以下示例不传递options对象。

相关问题