学习 Java 第一天:从零开始搭建项目,实现第一个接口(IDEA)

* 前言 <https://blog.csdn.net/weixin_44135121/article/details/92801713#_1>
* 一、从零开始搭建项目
<https://blog.csdn.net/weixin_44135121/article/details/92801713#_3>
* 二、从零开始实现第一个接口
<https://blog.csdn.net/weixin_44135121/article/details/92801713#_18>
* 三、学习总结 <https://blog.csdn.net/weixin_44135121/article/details/92801713#_102>


<>前言


虽然我是一枚妥妥的前端无疑了,但是前端和后端的关系是密不可分的,数据对接是每个前后端的家常便饭,我相信学习一门后端语言会有助于前后端地沟通。好的程序员是不分前后端的。只是侧重点不同而已。所以决定从今天开始,拓展一下语言的宽度,来学一下
Java,在此记录一下学习成果。本文适合 Java 菜鸟看,如果你是一枚老司机,欢迎多多指点。

<>一、从零开始搭建项目

* 打开 IDEA 编辑器,选择 File -> New -> Project;

* 选择 Spring Initializr ,点击 Next;

* 填写 Group( maven项目中的唯一坐标,可以由组织,比如 com ,和公司名称组成 )及 Artifact(项目名称),点击 Next;

* 选择 Web,选择 Spring Web Starter,点击 Next;

* 选择项目名称和项目存放路径,点击 Finish 完成;

* 以上步骤操作完之后,项目搭建就完成了,有时候 IDEA 会根据 pom.xml 中的配置下载依赖,如果没有自动下载,可手动下载,如下图所示;

* 下载完成之后,可将 .mvn 文件,mvnw 文件,mvnw.cmd 文件删除。

<>二、从零开始实现第一个接口

实现接口

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

* 在 com.example.demo 文件包下新建 entity 文件包;在此文件包下新建 User 类,在 User 类中分别定义 name 和
password 属性,内容如下; package com.example.demo.entity; public class User { String
name; int password; public String getName(){ return name; } public void setName(
String name){ this.name = name; } public int getPassword(){ return password; }
public void setPassword(String password){ this.password = password; } public
StringtoString(){ return "user{name='"+name+"\',"+"password="+password+"}"; } }
* 在 com.example.demo 文件包下新建 service 文件包;在此文件包下新建 UserService 接口,在 UserService
接口中定义 getUserInfo 方法,内容如下: package com.example.demo.service; import com.example.
demo.entity.User; public interface UserService { public User getUserInfo(); }
随后在 service 文件包中新建 impl 文件包,在 impl 文件包中新建 UserServiceImpl 来实现 UserService
接口,内容如下:
package com.example.demo.service.impl; import com.example.demo.entity.User;
import com.example.demo.service.UserService; import org.springframework.
stereotype.Service; @Service public class UserServiceImpl implements UserService
{ public User getUserInfo(){ User user = new User(); user.setName("jack"); user.
setPassword(12341234); return user; } } .idea
* 在 com.example.demo 文件包下新建 controller 文件包;在此文件包下新建 UserController
类,定义接口路径,返回接口数据,内容如下: package com.example.demo.controller; import com.example.
demo.entity.User; import com.example.demo.service.UserService; import org.
springframework.beans.factory.annotation.Autowired; import org.springframework.
web.bind.annotation.RequestMapping; import org.springframework.web.bind.
annotation.RequestMethod; import org.springframework.web.bind.annotation.
RestController; @RestController public class UserController { @Autowired
UserService service; @RequestMapping(value = "/getUserItem",method =
RequestMethod.GET) public String getUserItem(){ User user = service.getUserInfo(
); return user.toString(); } }
验证接口

* 执行主函数

* 在地址栏输入 http://localhost:8080/getUserItem <http://localhost:8080/getUserItem>
进行验证,结果正确。

<>三、学习总结

* 在搭建项目过程中,如果出现 “Initialization failed for ‘https://start.spring.io
<https://start.spring.io>’”的问题,可以参考博客 解决 IDEA 创建 spring 项目时出现 Initialization
failed for ‘https://start.spring.io’ 问题
<https://blog.csdn.net/weixin_44135121/article/details/92795467>。
* 在实现接口的过程中,需要在项目目录下,新建三个文件包:
* entity
包,也就是实体类,这里存放业务逻辑所需要的对象属性或方法。比如要获取用户信息,而用户信息包含名称和密码这两个属性,就需要在这里定义和创建,并同时定义 set
方法用于赋值,定义 get 方法用于取值。
* service 包,也就是提供服务的类,有了对象,接下来就要使用对象,实现功能,定义接口。定义接口之后还需要一个实现类,来实现接口。
* controller 包,提供了服务之后就要开始使用服务,也就是调用服务类所提供的方法来返回最终的数据。

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信