0

I'm in the process of creating a prototype for a web app, where one of the functions should allow someone to upload an excel file with some extra info. The file alongside the info is stored in an object, which is serialized and stored.

I have created the upload method, and while trying to test the function it throws the "Request method 'POST' not supported" whitelabel error page.

I have suspicions that it might be the pom.xml at fault, but I'm not entirely sure.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>project</groupId>
    <artifactId>answers</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MVC 1.0 Blank Project (from https://github.com/making/mvc-1.0-blank)</name>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.ozark</groupId>
            <artifactId>ozark</artifactId>
            <version>1.0.0-m02</version>
        </dependency>
                <!-- Swing exercise -->
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!-- Web exercise -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Here is the Controller class. We will move to a Thymeleaf configuration in a day or two, this html is just for testing purposes currently.



    @MultipartConfig
    @RestController
    @RequestMapping(value = "/Teacher", produces = "text/html;charset=UTF-8")
    public class Teacher {



        @GetMapping("")
        @ResponseBody
        TestController testcont;
        public String homePage(@RequestParam(value = "file", required = false) String name, HttpServletRequest request,
                HttpServletResponse response){

            StringBuilder sb = new StringBuilder();


            sb.append("<p> <a href='/Teacher/NewTest'>New Test upload</a></p>\n"
                    + "<p><a href='/SelectTest'>Select Test File</a> <button type='button'>Send Test</button></p>"
                    + "\n \n \n"
                    + "<p><a>Current Test for students:</a>\n <a href='/getCurrentTest'></a></p>"
                    );

            return sb.toString();
        }


        @GetMapping("/NewTest")
        @ResponseBody
        public String newTestUpload(HttpServletRequest request, HttpServletResponse response){
            StringBuilder sb = new StringBuilder();



            if(!request.getParameterNames().hasMoreElements()){
                sb.append("<p><form action='' method='post' enctype='multipart/form-data'>"
                        + "<label>Enter file</label><input type='file' name='file'>"
                        + "<button type='submit'>Upload</button></p>"

                        + "<p><form action='/testName'>Test Name: <input type='text' name='name' value=''></p>"

                        + "<p><form action='/addInfo'>Comment: <input type='text' comment='comment' value=''></p>"

                        + "<p>Answer 1: <input type='text' Answer='answer1' value=''></p>"

                        + "<p>Answer 2: <input type='text' Answer='answer2' value=''></p>"

                        + "</form>"

                        + "<a href='/Teacher'>Back</a>\n"
                        );
                return sb.toString();
            }
            else if(request.getParameter("file") != null && request.getParameter("name") != ""
                    && request.getParameter("comment") != "" && request.getParameter("answer1") != null
                    && request.getParameter("answer2") != null){

                try{
                    // Upload happens here
                    Part filePart = request.getPart("file");
                    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
                    InputStream fileContent = filePart.getInputStream();

                    File testExcel = new File(fileName);
                    testExcel.createNewFile();

                    Files.copy(fileContent, Paths.get(testExcel.getName()), StandardCopyOption.REPLACE_EXISTING);


                    double ans1 = Double.parseDouble(request.getParameter("answer1"));
                    double ans2 = Double.parseDouble(request.getParameter("answer2"));

                    Test test = new Test(testExcel, request.getParameter("name"), 
                                        request.getParameter("comment"), ans1, ans2);

                    testcont.addTest(test);

                    sb.append("New test uploaded!<br/>\n<a href='/'>Back</a>\n");
                    return sb.toString();


                } catch (Exception e){
                    sb.append("<h1>Couldnt insert test</h1>");
                    response.setStatus(HttpServletResponse.SC_OK);
                    return sb.toString();
                }

            }
            else{
                sb.append("failed<br/>\n<a href='/'>Back</a>\n");
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                return sb.toString();
            }

        }

    }

1
  • What do you expect from a request to upload a file that's mapped with @GetRequest? A request to upload a file should always be mapped with @PostRequest. Commented Jul 18, 2019 at 10:04

3 Answers 3

1

From the controller code you've posted, both of your endpoints are GET. /Teacher :- @GetMapping /Teacher/NewTest :- @GetMapping

You are trying to POST to an endpoint which does not support POST method, which is why you see error message, specifying 'POST' method not supported.

Sign up to request clarification or add additional context in comments.

1 Comment

Doh! That fixed it. Now I just need to figure out why it goes to the else statement "failed".
1

Both methods in your controller has @GetMapping annotation, that means that they support GET requests. You need to replace one with @PostMapping(to support POST requests)

Comments

1

Correct your method to:

@PostMapping
@RequestMapping("/NewTest")
public String newTestUpload(HttpServletRequest request, HttpServletResponse response) {

or

@PostMapping(path = "/NewTest")
public String newTestUpload(HttpServletRequest request, HttpServletResponse response) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.