0

При использовании интерфейса CommandLineRunner приложение работает как положено:

    @SpringBootApplication
public class SampleXMLApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SampleXMLApplication.class, args);
    }
    private final AuthorDao authorDao;
    private final ArticleDao articleDao;
    public SampleXMLApplication(AuthorDao authorDao, ArticleDao articleDao) {
        this.authorDao = authorDao;
        this.articleDao = articleDao;
    }
    @Override
    public void run(String... args) throws Exception {
        System.out.println(this.authorDao.selectAuthorById(2));
        System.out.println(this.articleDao.selectByAuthorId(2));
    }
}

Результат:

2018-04-22 19:21:46.154 DEBUG 11964 --- [           main] s.m.m.AuthorMapper.selectAuthorById      : ==>  Preparing: select * from author where author_id = ? 
2018-04-22 19:21:46.194 DEBUG 11964 --- [           main] s.m.m.AuthorMapper.selectAuthorById      : ==> Parameters: 2(Long)
2018-04-22 19:21:46.209 TRACE 11964 --- [           main] s.m.m.AuthorMapper.selectAuthorById      : <==    Columns: author_id, name, surname
2018-04-22 19:21:46.210 TRACE 11964 --- [           main] s.m.m.AuthorMapper.selectAuthorById      : <==        Row: 2, Eric, Pugh
2018-04-22 19:21:46.221 DEBUG 11964 --- [           main] s.m.m.AuthorMapper.selectAuthorById      : <==      Total: 1
[0,Eric,Eric,Pugh]
2018-04-22 19:21:46.224 DEBUG 11964 --- [           main] s.m.m.ArticleMapper.selectByAuthorId     : ==>  Preparing: select * from articles where author_id = ? 
2018-04-22 19:21:46.224 DEBUG 11964 --- [           main] s.m.m.ArticleMapper.selectByAuthorId     : ==> Parameters: 2(Long)
2018-04-22 19:21:46.226 TRACE 11964 --- [           main] s.m.m.ArticleMapper.selectByAuthorId     : <==    Columns: article_id, title, category, author_id
2018-04-22 19:21:46.226 TRACE 11964 --- [           main] s.m.m.ArticleMapper.selectByAuthorId     : <==        Row: 2, Hibernate HQL, Hibernate, 2
2018-04-22 19:21:46.227 TRACE 11964 --- [           main] s.m.m.ArticleMapper.selectByAuthorId     : <==        Row: 3, Java Concurrency, Java, 2
2018-04-22 19:21:46.228 DEBUG 11964 --- [           main] s.m.m.ArticleMapper.selectByAuthorId     : <==      Total: 2

При создании контроллера

@Controller
@RequestMapping(value="/rest")
public class AuthorController {

    @Autowired
    private AuthorDao authorDao;

    @GetMapping("articlesByAuthor/{authorId}")
    public List<Author> selectAuthorById(long id) {
      return authorDao.selectAuthorById(id);
    }
} 

Программа ничего не выполняет:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.10.RELEASE)


Process finished with exit code 0

В чем может быть проблема? Ссылка на исходники https://github.com/sovat/MyBatis_Spring_Boot

1 ответ 1

1

Вам нужно в зависимостях указать spring-boot-starter-web, а не spring-web:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring Boot Web Starter встроит в ваше приложение сервер (по умолчанию - Tomcat), который будет слушать нужный порт и отвечать на запросы по указанным в контроллерах адресам. Spring Web, хотя и содержит классы, помогающие просто построить REST-контроллеры, сам сервер не поднимает. Приложение Spring Web нужно паковать в WAR, прописывать точки входа в web.xml и самостоятельно деплоить в контейнер сервлетов (тот же Tomcat).

1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.