java - Postman 请求返回 406 错误,仍然有效

标签 java spring spring-boot postman

我目前正在编写一个小型 Spring Boot 应用程序,它从 postman POST 请求中获取几个简单的参数并将它们写入文本文件。

请求进展顺利,但 postman 返回此响应:

{
"时间戳": "2022-10-07T20:57:05.951+00:00",
“状态”:406,
“错误”:“ Not Acceptable ”,
“路径”:“/api/entry”
}

这是我的 Controller 类:

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>

感谢任何/所有输入。

最佳答案

在后期映射中执行此操作:

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

关于java - Postman 请求返回 406 错误,仍然有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73992673/

相关文章:

Java Hibernate JPQL 查询(聚合函数 : count)

java - 为什么 char[] 优于 String 作为密码?

spring - 在persistence.xml 中使用没有<class> 的Spring 持久性Xml 位置

spring - SpringBoot 1.5.* 上的执行器日志文件不起作用。 HTTP 404

angularjs - spring-boot:向tomcat服务器添加应用

java - 使用 Selenium Webdriver 和 Java 检查特定 H3 文本中的输入复选框

java - ibatis spring java.lang.NoSuchMethodError com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse

xml - Eclipse Spring STS "Config Set"有什么用?

spring-boot - 如何禁用 Javers 进行集成测试?

JavaFX FileChooser 在窗口为 "closed"后停止所有其他代码