当前位置:   article > 正文

使用Intellij idea创建一下java后台项目并实现第一个接口_idea后端实例接口教程

idea后端实例接口教程

创建java后台项目

1.下载并安装Intellij idea.

2.选择Create New Project

3勾选左侧的Spring Initializr ,点击next

4.填写项目名称和包名,点击next

5.勾选左侧web  ,然后勾选中间的sping web,点击右侧的next

6.等待项目创建完成。

从零开始实现第一个接口

实现接口

实现一个获取单条用户信息的接口 getUserItem,访问地址为 http://localhost:8080/getUserItem。

  1. 在 com.example.demo 文件包下新建 entity 文件包;在此文件包下新建 User 类,在 User 类中分别定义 name 和 password 属性,内容如下
    1. package com.example.demo.entity;
    2. public class User {
    3. String name;
    4. int password;
    5. public String getName(){
    6. return name;
    7. }
    8. public void setName(String name){
    9. this.name = name;
    10. }
    11. public int getPassword(){
    12. return password;
    13. }
    14. public void setPassword(String password){
    15. this.password = password;
    16. }
    17. public String toString(){
    18. return "user{name='"+name+"\',"+"password="+password+"}";
    19. }
    20. }

     

  2. 在 com.example.demo 文件包下新建 service 文件包;在此文件包下新建 UserService 接口,在 UserService 接口中定义 getUserInfo 方法,内容如下:
    1. package com.example.demo.service;
    2. import com.example.demo.entity.User;
    3. public interface UserService {
    4. public User getUserInfo();
    5. }

    随后在 service 文件包中新建 impl 文件包,在 impl 文件包中新建 UserServiceImpl 来实现 UserService 接口,内容如下:

    1. package com.example.demo.service.impl;
    2. import com.example.demo.entity.User;
    3. import com.example.demo.service.UserService;
    4. import org.springframework.stereotype.Service;
    5. @Service
    6. public class UserServiceImpl implements UserService {
    7. public User getUserInfo(){
    8. User user = new User();
    9. user.setName("jack");
    10. user.setPassword(12341234);
    11. return user;
    12. }
    13. }

     

  3. 在 com.example.demo 文件包下新建 controller 文件包;在此文件包下新建 UserController 类,定义接口路径,返回接口数据,内容如下:
    1. package com.example.demo.controller;
    2. import com.example.demo.entity.User;
    3. import com.example.demo.service.UserService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RequestMethod;
    7. import org.springframework.web.bind.annotation.RestController;
    8. @RestController
    9. public class UserController {
    10. @Autowired
    11. UserService service;
    12. @RequestMapping(value = "/getUserItem",method = RequestMethod.GET)
    13. public String getUserItem(){
    14. User user = service.getUserInfo();
    15. return user.toString();
    16. }
    17. }

    验证接口

  4. 执行主函数

  5. 在这里插入图片描述
  6. 在地址栏输入 http://localhost:8080/getUserItem 进行验证,结果正确。

在这里插入图片描述

 

学习总结

在实现接口的过程中,需要在项目目录下,新建三个文件包:
entity 包,也就是实体类,这里存放业务逻辑所需要的对象属性或方法。比如要获取用户信息,而用户信息包含名称和密码这两个属性,就需要在这里定义和创建,并同时定义 set 方法用于赋值,定义 get 方法用于取值。
service 包,也就是提供服务的类,有了对象,接下来就要使用对象,实现功能,定义接口。定义接口之后还需要一个实现类,来实现接口。
controller 包,提供了服务之后就要开始使用服务,也就是调用服务类所提供的方法来返回最终的数据。
 

发布jar包:

1.打开右侧的Maven,选择package打出jar包

2.打开cmd ,打开jar包所在的文件夹 

3.输入java -jar jar包名称

4.回车等待jar发布

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/584647
推荐阅读
相关标签
  

闽ICP备14008679号