Home > Software engineering >  Spring tool: Exception encountered during context initialization - Unable to start web server; neste
Spring tool: Exception encountered during context initialization - Unable to start web server; neste

Time:09-21

I am trying to get a simple Spring Boot to run. It does run and work if in the main I have it set to HelloWorldRest1Application.class. Once I change it to HelloWorld I get and can't see it on the localhost.

2021-09-19 19:36:40.009 INFO 1664 --- [ restartedMain] c.sw409.demo.HelloWorldRest1Application : Starting HelloWorldRest1Application using Java 16.0.1 on DESKTOP-55I895V with PID 1664 (C:\Users\ncost\Desktop\Fall 21\Advanced Java\java\HelloWorldRest1\target\classes started by ncost in C:\Users\ncost\Desktop\Fall 21\Advanced Java\java\HelloWorldRest1) 2021-09-19 19:36:40.013 INFO 1664 --- [ restartedMain] c.sw409.demo.HelloWorldRest1Application : No active profile set, falling back to default profiles: default 2021-09-19 19:36:40.045 INFO 1664 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2021-09-19 19:36:40.160 INFO 1664 --- [ restartedMain] c.sw409.demo.HelloWorldRest1Application : Started HelloWorldRest1Application in 0.52 seconds (JVM running for 1.366)


Main

package com.sw409.demo;

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

import com.sw409.HelloWorldRest.models.HelloWorldBean;
import com.sw409.HelloWorldRest.services.HelloWorld;

@SpringBootApplication
public class HelloWorldRest1Application {

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

}

HelloWorldBean

package com.sw409.HelloWorldRest.models;

public class HelloWorldBean {

    String mes;

    public HelloWorldBean(String str) {

        this.mes = str;
    }

    public String getMes() {
        return mes;
    }

    public void setMes(String mes) {
        this.mes = mes;
    }

    public String toString() {
        return mes;
    }
}

HelloWorld

package com.sw409.HelloWorldRest.services;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.sw409.HelloWorldRest.models.HelloWorldBean;

@RestController
public class HelloWorld {
    @GetMapping("/hello")
    public String helloworld() {

        return ("hello world");
    }

    @GetMapping("/hello/{name}")
    public String helloworld(@PathVariable("name") String str) {

        return "hello "   str;
    }

    @GetMapping("/helloworldbean/{name}")
    public HelloWorldBean hello(@PathVariable("name") String str) {
        return new HelloWorldBean("Hello everyone! "   str);
    }
}

CodePudding user response:

Move HelloWorldRest1Application to package com.sw409 and set HelloWorldRest1Application as your main class as follows:

package com.sw409;

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

import com.sw409.HelloWorldRest.models.HelloWorldBean;
import com.sw409.HelloWorldRest.services.HelloWorld;

@SpringBootApplication
public class HelloWorldRest1Application {

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

@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes. The default value for @ComponentScan means that all the sub packages on the package the @ComponentScan is used are scanned. That is why it is usually a good practice to include the main class in the base package of the project.

  • Related