0

When trying to run a simple app on spring framework I bumped into a problem with importing web package:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

my code is really basic, I'm just trying to post back a simple txt

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        AppServerHandler handler = AppServerHandler.getInstance(6666);


    }

    @RequestMapping(value = "/",method = POST)   <--- not familiar with annonation
    public String index() {
        return AppServerHandler.getWelcomeMsg();
    }

}

pom.xml

<?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>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


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

</project>

I'm really curious to know what can go wrong. Thanks.

ishaishai
  • 73
  • 9
  • Can you show your pom.xml – Amit Phaltankar Dec 04 '19 at 01:01
  • have a look I've added it to the post – ishaishai Dec 04 '19 at 01:03
  • Remove the `spring-web` and `spring-webmvc` dependencies, those are already included in the `spring-boot-starter-web` dependency. Then run `mvn dependency:purge-local-repository` and re-import your project **as Maven project** in your IDE (or run `mvn verify` from cmd line to re-download the dependencies). – M. Deinum Dec 04 '19 at 06:49
  • mvn verify outputs: [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? [ERROR] [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException – ishaishai Dec 04 '19 at 12:43
  • change library from JRE to JDK in your Java Build Path. you can refer [answer](https://stackoverflow.com/questions/19655184/no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-ra) – Dhruvam Gupta Dec 05 '19 at 06:35

1 Answers1

-1

please include @RestController in your class like below.

@RestController
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        AppServerHandler handler = AppServerHandler.getInstance(6666);


    }

    @GetMapping(value = "/")   <--- use this annotation instead of @Requestmapping
    public String index() {
        return AppServerHandler.getWelcomeMsg();
    }

}

@GetMapping has HTTP method GET.

imprezzeb
  • 706
  • 1
  • 7
  • 18
  • can't resolve ``` @RestController and @GetMapping ``` – ishaishai Dec 04 '19 at 01:08
  • Your imports are correct for RestController and RequestMapping. When are you seeing the error, is it during mvn clean install or just in editor? – imprezzeb Dec 04 '19 at 01:12
  • in Intellij build log: Error:(5, 47) java: package org.springframework.web.bind.annotation does not exist Error:(6, 47) java: package org.springframework.web.bind.annotation does not exist Error:(7, 2) java: cannot find symbol symbol: class RestController Error:(18, 10) java: cannot find symbol symbol: class GetMapping location: class com.FileServer.FileServer.DemoApplication Error:(18, 42) java: cannot find symbol symbol: variable POST location: class com.FileServer.FileServer.DemoApplication – ishaishai Dec 04 '19 at 01:14
  • Change your groupId to your organization instead of org.springframework – imprezzeb Dec 04 '19 at 01:19
  • To which organization -\_/- – ishaishai Dec 04 '19 at 01:20
  • org.yourorganization or org.yourcompanyname – imprezzeb Dec 04 '19 at 01:21