如何创建spring restful api

最近在研究spring框架,今天试着写了一个简单的user操作的restful api, 以下是创建的步骤:

1.创建Maven project

可以用IntelliJ IDEA来创建一个Maven project, 然后添加dependency和plugin:

<?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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.3.6.RELEASE</version>
  <relativePath />
  <!-- lookup parent from repository -->
 </parent>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>
 <dependencies>
  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
  </dependency>
  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional>
  </dependency>
 </dependencies>
 <build>
  <plugins>
     <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
           <fork>true</fork>
        </configuration>
     </plugin>
  </plugins>
 </build>
</project>

2.创建project structure

以下是我创建的project结构:

Paste_Image.png

3.创建类文件

1)User.java

@Component
public class User {    
    private Long id;    
    private String name;    
    private int age;    
    public String getName() {        return name;    }    
    public void setName(String name) {        this.name = name;    }
    public int getAge() {        return age;    }          
    public void setAge(int age) {        this.age = age;    }    
    public Long getId() {        return id;    }    
    public void setId(Long id) {        this.id = id;    }    
}

2)UserController.java

@RestController
@RequestMapping(value = "/users")
public class UserController {    
    static Map<Long, User> userList = Collections.synchronizedMap(new HashMap<>());    

@RequestMapping(value = "/", method = RequestMethod.GET)
public List<User> getUserList(){        
    List<User> users = new ArrayList<User>(userList.values());        
    return users;    
}    
@RequestMapping(value = "/", method = RequestMethod.POST)    
public String postUser(@ModelAttribute User newUser){
    userList.put(newUser.getId(), newUser);   
    return "successful";    
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable Long id){
    return userList.get(id);    
}    
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String putUser(@PathVariable Long id, @ModelAttribute User user){
    User u = userList.get(id);
    u.setName(user.getName());
    u.setAge(user.getAge());
    userList.put(id,u);
    return "successful";    
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String deleteUser(@ModelAttribute Long id){
    userList.remove(id);
    return "successful";
  }
}

3)Application.java

这是程序启动的入口

@SpringBootApplicationpublic class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

另外有些童鞋可能对文件的归类不是很明白,可以参考以下这段话:

Spring MVC中往往分为Controller(具体接口,处理相应逻辑),dao(数据库工具类),entity(实体类,对应数据库中的表),service(服务类,调用数据库工具类进行操作),其中Service和Dao部分通常通过接口和继承接口的实体类实现

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,959评论 6 342
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan阅读 5,426评论 7 56
  • 我有个朋友叫小柒,可以说是我最熟悉的朋友。小柒人很好,很可爱。人送外号小太阳,是个讨人喜欢的女孩儿。 小柒有一头又...
    夏染凉阅读 255评论 0 1
  • 此句原是《神雕侠侣》中,天竺神僧去绝情谷给杨过找情花毒解药时所说。今天被我借用了。 前两天吹空调太多,昨天早起感冒...
    Angel刘咏霞阅读 3,794评论 0 0