<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
@RestController @EnableAutoConfiguration public class SampleController { @RequestMapping("/sample") public String hello(){ return "sample"; } public static void main(String[] args) { SpringApplication.run(SampleController.class, args); } }
啓動main函數後,能夠在控制檯看到啓動了一個Tomcat容器,經過瀏覽器訪問 sample便可看到頁面輸出了SampleController中hello方法的返回值了。java
至此一個簡單的Rest風格應用已經搭建成功。mysql
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
在resources文件夾下建立application.yml文件並添加如下數據庫配置web
spring: datasource: url: jdbc:mysql://localhost/test username: root password: ****** driver-class: com.mysql.jdbc.Driver
在mysql中執行如下sqlspring
use test; create table user( id int primary key not null, username varchar(20), create_date date, last_accessed date, is_active tinyint(1) ); insert into user values(1,'zhangsan',now(),now(),1); insert into user values(2,'lisi',now(),now(),1); insert into user values(3,'wangwu',now(),now(),0);
@Entity @Table(name="user") public class User { @Id @GeneratedValue private Long id; private String username; private Date createDate; private Date lastAccessed; boolean isActive; //getter //setter }
@Repository public interface UserRepository extends CrudRepository<User, Long>{ }
@EntityScan("com.rft.springboot.entity") @EnableJpaRepositories("com.rft.springboot.repository") @RestController @EnableAutoConfiguration @RequestMapping("/user") public class UserController { @Autowired private UserRepository userRepository; @RequestMapping("/{id}") public User view(@PathVariable("id") Long id){ return userRepository.findOne(id); } @RequestMapping("/all") @ResponseBody Iterable<User> get(){ return userRepository.findAll(); } public static void main(String[] args) { SpringApplication.run(UserController.class, args); } }
運行main方法,在瀏覽器地址欄輸入 http://localhost:8080/user/1和 http://localhost:8080/user/all便可查詢到數據庫中的數據sql
Spring Boot支持功能強大的Spring Security,而且提供了自動配置的功能,以快速簡便地啓用安全層。數據庫
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
在application.yml中配置Basic認證瀏覽器
security: basic: enabled: true user: name: test password: test
運行UserController中的main方法,再次訪問 http://localhost:8080/user/1,頁面會彈出Basic認證框tomcat
<packaging>war</packaging> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
其中scope爲provided表示由環境提供安全
@RestController @SpringBootApplication public class Application extends SpringBootServletInitializer{ @RequestMapping("/index") public String index(){ return "index"; } public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for(String beanName : beanNames){ System.out.println(beanName); } } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
運行main方法,控制檯會打印出全部beanspringboot
使用命令提示符在工程所在目錄下運行
mvn package
將打包好的war包拷貝到Tomcat下,啓動Tomcat服務,經過瀏覽器訪問http://localhost:8080/springboot/user/1,結果正常。
參考資料: