第一步:创建项目
mvn archetype:generate -DgroupId=com.w3c0.app -DartifactId=myapp-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -Dversion=1.0
第二步:验证正确性
mvn package
第三步:修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.w3c0.app</groupId> <artifactId>myapp</artifactId> <packaging>jar</packaging> <version>1.0</version> <name>myapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Additional lines to be added here... --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
第四步:查看并下载依赖
mvn dependency:tree
第五步:创建user类
package com.w3c0.app; import javax.persistence.*; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Entity @Table(name = "tf_user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; public User() { } public User(long id) { this.id = id; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; return true; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
第六步:创建controller类
package com.w3c0.app; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; @RestController @RequestMapping("/user") public class UserController { @RequestMapping("/{id}") public User view(@PathVariable("id") Long id) { User user = new User(); user.setId(id); user.setName("zhang"); user.setEmail("zhang@qq.com"); return user; } }
第七步:修改app类
package com.w3c0.app; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @RestController /*@Configuration @EnableAutoConfiguration @ComponentScan*/ @SpringBootApplication /*等价上面3个 */ public class App { public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
第八步:运行
mvn spring-boot:run
显示如下图示:
访问:http://localhost:8080/user/1 输出:
{"id":1,"name":"zhang","email":"zhang@qq.com"}
W3c0.com 提供的内容仅用于培训。我们不保证内容的正确性。通过使用本站内容随之而来的风险与本站无关。W3c0 简体中文版的所有内容仅供测试,对任何法律问题及风险不承担任何责任。 当使用本站时,代表您已接受了本站的使用条款和隐私条款。版权所有,保留一切权利。 鲁ICP备15022115号