Dale Dale - 5 months ago 30
Java Question

How can I move the main method that sets up Spring Boot into the base package without breaking it

I'm checking out Spring Boot and I'm pretty impressed. Very quick development. I have an application, however, all the examples have the main method within the controller package. [E.g. com.demo] I'd like to move the controller into its own package. [e.g. com.demo.controller]. And then leave the main method in the base package [com.demo]. When I do this, it breaks the world. It seems that some of the annotations might need to be separated out or the convenience method might need to be broken up. I've moved my Application.java into a base package as I stated abive and moved what I believed were the correct annotations however it seems I'm missing something. Here's my code.

package com.demo;

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

import com.demo.controllers.StudentController;

@SpringBootApplication
@EnableAutoConfiguration
public class Application {

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


My controller is as follows

package com.demo.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.demo.models.Student;

@Controller
@RequestMapping("/student")
public class StudentController {

@RequestMapping(method = RequestMethod.GET)
public List<Student> getAll() {
return new ArrayList<Student>();
}

@RequestMapping(method = RequestMethod.POST)
public Student create(@RequestBody Student Student) {
return null;
}

@RequestMapping(method = RequestMethod.DELETE, value = "{id}")
public void delete(@PathVariable String id) {

}

@RequestMapping(method = RequestMethod.PUT, value = "{id}")
public Student update(@PathVariable String id, @RequestBody Student Student) {
return null;
}
}


The stack trace I'm getting is this.

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at com.demo.Application.main(Application.java:14) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
... 8 common frames omitted


Not sure if I'm missing an annotation or need to split up some of the convenience annotations.

Answer

It is not SpringApplication.run(StudentController.class, args); but SpringApplication.run(Application.class, args);.

Follow the guide carefully.