컨트롤러를 통해 API를 제공하는 작은 스프링 부팅 앱이 있습니다. 컨트롤러는 사용자 mongodb 리포지토리가 사용하는 일부 서비스를 사용합니다.스프링 부트 웹 앱이 시작되지 않음
내 pom.xml 파일 :
<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>com.springboot.testapp</groupId>
<artifactId>event-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- MONGO DB -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
컨트롤러 :
@Controller
public class EventController {
@Autowired
private EventService eventService;
@Autowired
private EventAttendantService eventAttendantService;
@Autowired
private EventDayService eventDayService;
@GetMapping(value = "/event/{id}")
@ResponseBody
public Event getEventById(@PathVariable Long id) {
return eventService.getEventById(id);
}
@GetMapping(value = "/eventDays")
@ResponseBody
public List<EventDay> getEventDays() {
return eventDayService.getAllEventDays();
}
@GetMapping(value = "/eventDay/{id}")
@ResponseBody
public List<Event> getEventsForDay(@PathVariable Long eventDayId) {
EventDay eventDay = eventDayService.findById(eventDayId);
List<Event> events = eventDay.getEvents()
.stream()
.map(id -> eventService.getEventById(id))
.collect(Collectors.toList());
events.sort(new Comparator<Event>() {
@Override
public int compare(Event o1, Event o2) {
return o1.getStartTime().compareTo(o2.getStartTime());
}
});
return events;
}
@GetMapping(value = "/event/{id}/attendants")
@ResponseBody
public List<EventAttendant> getAttendantsForEvent(@PathVariable Long eventId) {
Event event = eventService.getEventById(eventId);
return event.getAttendants()
.stream()
.map(id -> eventAttendantService.getById(id))
.collect(Collectors.toList());
}
@PostMapping(value = "/eventDay")
@ResponseBody
public EventDay createEventDay(@RequestParam(value = "date") String date) {
return eventDayService.createEventDay(date);
}
@DeleteMapping(value = "/eventDay/{eventDayId}")
public void removeEventDay(@PathVariable(value = "eventDayId") Long eventDayId) {
EventDay eventDay = eventDayService.findById(eventDayId);
eventDay.getEvents()
.stream()
.forEach(eventId -> eventService.deleteEvent(eventId));
eventDayService.removeEventDay(eventDayId);
}
@PostMapping(value = "/event")
@ResponseBody
public Event createEvent(@RequestParam(value = "eventDayId") Long eventDayId,
@RequestParam(value = "name") String name,
@RequestParam(value = "description") String description,
@RequestParam(value = "location") String location,
@RequestParam(value = "startTime") String startTime,
@RequestParam(value = "endTime") String endTime) {
Event event = eventService.createEvent(name, description, location, startTime, endTime);
eventDayService.addEvent(eventDayId, event.getId());
return event;
}
@DeleteMapping(value = "/event/{eventId}")
@ResponseBody
public void removeEvent(@PathVariable(value = "eventId") Long eventId) {
List<EventDay> allEventDays = eventDayService.getAllEventDays();
Optional<EventDay> eventDayForEvent = allEventDays.stream()
.filter(eventDay -> eventDay.getEvents().contains(eventId))
.findFirst();
if (eventDayForEvent.isPresent()) {
eventDayService.removeEvent(eventId, eventDayForEvent.get().getId());
}
}
@PostMapping(value = "/attendant")
@ResponseBody
public EventAttendant createAttendant(@RequestParam(value = "firstName") String firstName,
@RequestParam(value = "lastName") String lastName) {
return eventAttendantService.create(firstName, lastName);
}
@PostMapping("/event/assign")
public void assignAttendant(@RequestParam(value = "eventId") Long eventId,
@RequestParam(value = "attendantId") Long attendantId) {
eventService.addAttendant(eventId, attendantId);
}
@PostMapping("/event/remove")
public void removeAttendant(@RequestParam(value = "eventId") Long eventId,
@RequestParam(value = "attendantId") Long attendantId) {
eventService.removeAttendant(eventId, attendantId);
}
}
의 기본 응용 프로그램 클래스 :
@SpringBootApplication(scanBasePackages = "com.springboot.test")
public class EventsAPI {
private static final Logger LOG = LoggerFactory.getLogger(EventsAPI .class);
public static void main(String[] args) {
SpringApplication.run(EventsAPI .class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(9000);
factory.setSessionTimeout(10, TimeUnit.MINUTES);
return factory;
}
}
내가 MVN 봄 부팅을 실행하면 : 쉼표에서 실행 또는 Eclipse에서 모든 종류의 오류가 발생합니다. 나는 "servletContainer"방법을 제거하면, 내가 얻을 : 그것은이 있다면
org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
, 나는 또 다른 오류가 발생합니다 :
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletContainer' defined in com.centric.centricexpress.CentricExpressAPI: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.context.embedded.EmbeddedServletContainerFactory]: Factory method 'servletContainer' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/catalina/LifecycleListener
메이븐 의존성에 바람둥이 - 포함 코어-8.5.11 추가하고
, 찾을 수없는 것으로보고 된 클래스가 존재합니다. 이것은 나를 위해 매우 혼란 스럽다.
나는 또한 인터넷에서 발견 된 몇 가지 데모 프로젝트를 시도했지만 요청을 수신하는 내장 된 컨테이너로 스프링 부트 애플리케이션을 시작하고 실행할 수 없습니다.
누군가 내가 잘못하고있는 것을 지적 할 수 있습니까?
감사합니다.
를 실행하십시오? 언뜻보기에 코드가 정상적으로 보입니다. 'mvn spring-boot : run'을 실행하기 전에'mvn clean install'을 실행 해 주시겠습니까? – artemisian
stacktrace를 사용하여 답변을 작성하겠습니다. – Cristian