项目目录结构:
com +- xx +- application.java | +- controller | +- SimpleController .java | +- entity +- User.java1. Eclipse创建一个简单的Maven工程:
com +- example +- myPRoject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- web +- CustomerController.java2.修改pop.xml
把下面代码添加进去
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>3.实体类package com.xx.entity;public class User { private int id; private String name; public User() { } public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}4.Controller类package com.xx.controller;import org.springframework.web.bind.annotation.*;import com.xx.entity.User;@RestControllerpublic class SimpleController { @RequestMapping("/hello") String hello() { return "Hello World!"; } @RequestMapping("/user/{id}") User user(@PathVariable("id") int id) { User user = new User(id, "yourName"); return user; }}5.Application类package com.xx;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan@EnableAutoConfigurationpublic class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class); }}6.Spring Boot建议将我们main方法所在的主要的配置类配置在根包名下。
在这个工程里“main方法所在的主要的配置类”为Application类。(见顶部目录图)
新闻热点
疑难解答