Spring、Hibernate、RESTful Webサービス、Gradleについて学びたいと思っています。Spring、Hibernate、RESTful WebサービスとGradleを連携させる方法
私はこれらの2つのチュートリアルを紹介しました。
スプリング4 +アノテーション RESTfulなWebサービス
https://spring.io/guides/gs/rest-service/
構築
を用いて、4 +のGradleの統合例を休止状態Iは、ローカルコードをテストし、両方にコードが期待どおりに実行するインスタンス
私はこれらの2つのチュートリアルを、Springフレームワークを使用する1つの(構成された)プロジェクト、RESTful Webサービス経由でDBに接続するHibernate、ビルドツールとしてのGradleに組み合わせようとしています。言い換えれば
、私は以下を達成したいと思います:私は、URLを介して、次の要求を提出
:はlocalhost:?8080 /国COUNTRYNAME =フランス意志RESTfulなWebサービスを呼び出すために、私のデータベースに(Hibernateを介して)照会し、正しい結果をJSON形式で返します。
私はやや進歩しましたが、設定が正しくないようです。私のGradleのビルドは成功したが、私はローカルホストにアクセスしようとすると:8080 /国を私が取得:
このサイトに到達することはできません
localhostの接続を拒否しました。
ビルド: ERR_CONNECTION_REFUSED
は、ここに私のGradleのプロジェクトです。Gradleの
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
// DB
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
//archivesBaseName = 'SpringHibernateRESTGradle'
// version = '1.0-SNAPSHOT'
jar {
baseName = 'countries-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.1.4.RELEASE'
compile 'org.hibernate:hibernate-core:4.3.6.Final'
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.slf4j:slf4j-simple:1.7.7'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'mysql:mysql-connector-java:5.1.31'
compile 'commons-dbcp:commons-dbcp:1.4'
// ws
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
のAppConfig
package com.pckg.config;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.pckg.dao.CountryDAO;
import com.pckg.dao.ICountryDAO;
import com.pckg.entity.Country;
@Configuration
@EnableTransactionManagement
public class AppConfig {
@Bean
public ICountryDAO getCountryDAO() {
return new CountryDAO();
}
@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/testing_db");
dataSource.setUsername("****");
dataSource.setPassword("****");
return dataSource;
}
@Bean
public SessionFactory getSessionFactory() {
SessionFactory sessionFactory = new LocalSessionFactoryBuilder(getDataSource()).addAnnotatedClasses(Country.class).buildSessionFactory();
return sessionFactory;
}
@Bean
public HibernateTemplate getHibernateTemplate() {
return new HibernateTemplate(getSessionFactory());
}
@Bean
public HibernateTransactionManager getHibernateTransManager() {
return new HibernateTransactionManager(getSessionFactory());
}
}
ServiceControllerの
package com.pckg.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.pckg.entity.Country;
@RestController
public class ServiceController {
@RequestMapping("/country")
public Country getCountryByName(@RequestParam(name = "countryname", required = false) String countryName) {
// AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// ctx.register(AppConfig.class);
// ctx.refresh();
//
// ICountryDAO countryDAO = ctx.getBean(ICountryDAO.class);
// Country country = countryDAO.getCountryByName(countryName);
// just for testing purposes - still doesn't work
Country c = new Country();
c.setId(2);
c.setCountry("France");
c.setCapital("Paris");
c.setContinent("Europe");
return c;
}
}
CountryDAO
package com.pckg.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import com.pckg.entity.Country;
@Transactional
public class CountryDAO implements ICountryDAO {
@Autowired
private HibernateTemplate hibernateTemplate;
@Override
public List<Country> getAllCountries() {
String query = "from Country";
List<Country> countries = (List<Country>) hibernateTemplate.find(query, null);
// print out
for (Country c : countries) {
System.out.print(c.getId() + " ");
System.out.print(c.getCountry() + " ");
System.out.print(c.getCapital() + " ");
System.out.println(c.getContinent() + " ");
}
return countries;
}
@Override
public List<Country> getCountriesByContinent(String continent) {
String query = "from Country c where c.continent = :cntnt";
List<Country> countries = (List<Country>) hibernateTemplate.findByNamedParam(query, "cntnt", continent);
// print out
for (Country c : countries) {
System.out.print(c.getId() + " ");
System.out.print(c.getCountry() + " ");
System.out.print(c.getCapital() + " ");
System.out.println(c.getContinent() + " ");
}
return countries;
}
@Override
public Country getCountryByName(String country) {
String query = "FROM Country c WHERE c.country = : cntry";
Country countryResult = (Country) hibernateTemplate.findByNamedParam(query, "cntry", country);
return countryResult;
}
}
ICountryDAO
package com.pckg.dao;
import java.util.List;
import com.pckg.entity.Country;
public interface ICountryDAO {
public List<Country> getAllCountries();
public List<Country> getCountriesByContinent(String continent);
public Country getCountryByName(String country);
}
国
package com.pckg.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "countries")
public class Country {
@Id
@Column(name = "id")
private int id;
@Column(name = "country")
private String country;
@Column(name = "capital")
private String capital;
@Column(name = "continent")
private String continent;
// no arg constructor
public Country() {
}
public Country(int id, String country, String capital, String continent) {
super();
this.id = id;
this.country = country;
this.capital = capital;
this.continent = continent;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
}
CountryService
package com.pckg.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.pckg.dao.ICountryDAO;
import com.pckg.entity.Country;
public class CountryService implements ICountryService {
@Autowired
private ICountryDAO countryDAO;
@Override
public List<Country> getAllCountries() {
return countryDAO.getAllCountries();
}
@Override
public List<Country> getCountriesByContinent(String continent) {
return countryDAO.getCountriesByContinent(continent);
}
@Override
public Country getCountryByName(String country) {
return countryDAO.getCountryByName(country);
}
}
IC ountryService
package com.pckg.service;
import java.util.List;
import com.pckg.entity.Country;
public interface ICountryService {
public List<Country> getAllCountries();
public List<Country> getCountriesByContinent(String continent);
public Country getCountryByName(String country);
}
アプリケーション
package com.pckg.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
私のコードの作業を取得する方法任意の提案をいただければ幸いです。ありがとう!
EDIT:
私は私のgradle.buildファイルに次のコードを追加しました:
configurations.all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.springframework.boot', module: 'logback-classic'
}
それは、元の問題を修正しましたが、私は "gradlew bootRun" を実行した後、新しいエラーを取得しています
[main] WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: 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.
[main] ERROR org.springframework.boot.SpringApplication - Application startup failed
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:137)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
at com.pckg.test.Application.main(Application.java:10)
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:189)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)
... 8 more
:bootRun FAILED
FAILURE: Build failed with an exception.
問題を調査しましたが、この問題の修正が見つかりませんでした。