postman 406错误,返回首页

wko9yo5t  于 2022-11-07  发布在  Postman
关注(0)|答案(2)|浏览(306)

我目前正在编写一个小型的Sping Boot 应用程序,它从 Postman POST请求中接收几个简单的参数,并将它们写入一个文本文件。
请求顺利通过,但 Postman 返回以下响应:
{
“时间戳”:“2022年10月7日星期五20:57:05.951+00:00”,
“状态”:406,
“错误”:“不可接受”,
“路径”:“/api/项目”
}
这是我的控制器类:

import com.WebExample.WebEx.FileWriter.WriteFiles;
import com.WebExample.WebEx.Input.IncomingData;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RequestMapping(value = "/api", method = RequestMethod.POST, consumes="application/json")
@RestController
public class MainController {
    @PostMapping("entry")
    @ResponseStatus(HttpStatus.CREATED)
    public WriteFiles getFileParams(@RequestBody IncomingData incomingData) {
        String name = incomingData.getName();
        int number = incomingData.getNumber();
        WriteFiles file = new WriteFiles(name, number);
        file.file(name,number);
        return file;
    }
}

和我的文件写入器类:

public class WriteFiles {
    public WriteFiles(final String name, final int number) {
    }

    public void file(String name, int number) {
        try {
            String header = "***Writing File for " + name + "***\n";
            String footer = "\n ---- FILE COMPLETE!  GENERATED ON: " + LocalDate.now();
            String detail = "TOTAL: --- " + number;
            System.out.println("AWAIT FILE GENERATION!");
            File myFile = new File("newFile.txt");
            if (myFile.exists()) {
                myFile = new File("newerFile" + number + ".txt");
            }
            FileWriter writeFile = new FileWriter(myFile);
            writeFile.write(header);
            writeFile.write("--------------------------\n");
                writeFile.write(detail + "\n");
                writeFile.write(detail + " DOUBLED! " + (number * 2) + "\n");
                writeFile.write(detail + " HALVED! " + (number / 2) + "\n");
                writeFile.write(detail + " SQUARED! " + number * number + "\n");
                writeFile.write(detail + " PLUS 78! " + (number + 78) + "\n");
                writeFile.write(detail + " MINUS 23! " + (number - 23) + "\n");

            writeFile.write("--------------------------\n");
            writeFile.write(footer);
            writeFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是来自 Spring Boot 控制台的错误:

WARN 36237 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

同样,它可以工作,但我试图解决这个错误。下面是我的POM.xml(使用JVM 17):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.WebExample</groupId>
    <artifactId>WebEx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>WebEx</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

感谢任何/所有意见。

gcuhipw9

gcuhipw91#

在后Map中执行以下操作:

import com.WebExample.WebEx.FileWriter.WriteFiles;
import com.WebExample.WebEx.Input.IncomingData;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RequestMapping(value = "/api", method = RequestMethod.POST,  consumes="application/json")
@RestController
public class MainController {
@PostMapping("/entry")
@ResponseStatus(HttpStatus.CREATED)
public WriteFiles getFileParams(@RequestBody IncomingData incomingData) {
        String name = incomingData.getName();
        int number = incomingData.getNumber();
        WriteFiles file = new WriteFiles(name, number);
        file.file(name,number);
        return "created";
    }
}
66bbxpm5

66bbxpm52#

它显示错误的原因是由于函数的返回类型。
您正在返回writefiles类型的对象,该对象除了函数之外没有任何数据,这就是它显示错误的原因。您可以将该方法从

public WriteFiles getFileParams(@RequestBody IncomingData incomingData) {
        String name = incomingData.getName();
        int number = incomingData.getNumber();
        WriteFiles file = new WriteFiles(name, number);
        file.file(name,number);
        return file;
    }

收件人:

@PostMapping("entry")
    @ResponseStatus(HttpStatus.CREATED)
    public string getFileParams(@RequestBody IncomingData incomingData) {
                String name = incomingData.getName();
                int number = incomingData.getNumber();
                WriteFiles file = new WriteFiles(name, number);
                file.file(name,number);
                return "file created";
            }

如果一切正常,它将在 Postman 中显示消息为文件已创建,状态代码为201
另外,如果需要,您可以在类Writefiles中创建一些变量,这些变量存储可以返回的名称和编号

相关问题