简体   繁体   中英

Postman Request Returns 406 Error, Still Works

I am currently writing a small spring boot application that takes in a couple of simple parameters from a postman POST request and writes them to a text file.

The requests are going through fine but postman returns this response:

{
"timestamp": "2022-10-07T20:57:05.951+00:00",
"status": 406,
"error": "Not Acceptable",
"path": "/api/entry"
}

This is my Controller Class:

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

And my File Writer Class:

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

This is the error from the spring boot console:

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

Again, it works but I'm trying to sort out this error? Here's my POM.xml (using 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>

Appreciate any/all input.

do this in postmapping:

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

The reason it is showing error is due to the return type of your function.

You are returning the object of type writefiles which do not have any data other than functions that is why it is showing the error. You can change the method from

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

To:

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

If everything works fine it will show the message in the postman as file created and status code 201

Also if you want you can create some variables in you class Writefiles which store name and number which can be returned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM