Home > Mobile >  Maven Spring Boot App Failing to Start from VS Code
Maven Spring Boot App Failing to Start from VS Code

Time:01-26

Attempting to write a Java RESTful API When I run mvn clean install I build no problem. When I run mvn spring-boot run I get the following runtime error. I have included the pom.xml and classes that I think are relevant. I also have a service layer and entity class not listed, though I don't think they are the issue. I can add if maybe someone would like to see them as well. Any help would be greatly appreciated.Thank you in advance.

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]

below is code that I think could potentially be causing the problem:

pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.myproject</groupId>
  <artifactId>Java_API</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.3.3.RELEASE</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
  <properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

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

Main.java

package com.example.myproject;

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

@SpringBootApplication
public class Main {
    
    public static void main(String[] args)
    {
        SpringApplication.run(myController.class,args);
    }
}

myController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class myController {


    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/employee/{id}")
    public Employee getEmployee(@PathVariable int id) {
        return employeeService.getEmployee(id);
    }

    @PostMapping("/employee")
    public void createEmployee(@RequestBody Employee employee) {
        EmployeeService.createEmployee(employee);
    }
}
  • I have tried only having starter-web instead of both starter-web and starter-tomcat, but it yields the same result. The versioning seems right.
  • tried adding <scope>provided</scope>, granted I'm not entirely sure what it does.
  • A number of other things that may come to me, blanking on everything I have tried. Really stumped on what it does not like.

CodePudding user response:

Change Main.java

change SpringApplication.run(myController.class,args);

to SpringApplication.run(Main.class, args);

public static void main(String[] args)
    {
        //SpringApplication.run(myController.class,args);
        SpringApplication.run(Main.class, args);
    }

pom.xml

you can remove spring-boot-starter-tomcat

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.myproject</groupId>
  <artifactId>Java_API</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.3.3.RELEASE</version>
    </dependency>
      <!--
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.3.3.RELEASE</version>
        <scope>provided</scope>
    </dependency>
    -->
  </dependencies>
  <properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

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

package and run

mvn clean package spring-boot:run

test

curl http://localhost:8080/employee/22

I can't get enough information from your question. I change my test code:

myController.java

package com.example.myproject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class myController {
    //@Autowired
    //private EmployeeService employeeService;

    @GetMapping("/employee/{id}")
    public Employee getEmployee(@PathVariable int id) {
        Employee e1=new Employee();
        e1.setId(id);
        e1.setName("Hello World");
        return e1;
        //return employeeService.getEmployee(id);
    }

    @PostMapping("/employee")
    public void createEmployee(@RequestBody Employee employee) {
        System.out.println("DEBUG-"  employee.getId() "\t" employee.getName());
        //EmployeeService.createEmployee(employee);
    }
}

Employee.java

package com.example.myproject;

public class Employee {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I use the following code to test on my machine and it can be executed. At the same time, when you change SpringApplication.run(Main.class, args); back to SpringApplication.run(myController.class, args) ;, when executed it throws the same error message as in the question, Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

If there are other error messages generated, it should be part of your EmployeeService. You did not provide enough information.

  • Related