记录(二):创建Spring 项目接口实现

第一步:创建项目

 打开 idea 

        File -->  new -->project -->Spring Initalizr -->next      选择Spring web 然后finish创建项目

 

 第二步:在src/main 目录下创建webapp (第二步为了验证创建项目是否正常)

 第三步:写接口文件

创建实体类  UserBean

package com.example.demo.entiy;

public class UserBean {
        private String name;
        private String password;
        public String getName(){
            return  name;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getPassword(){
            return  password;
        }
        public void setPassword(String password){
            this.password = password;
        }
        public String toString(){
            return "{name:\'"+name+"\',"+"password:\'"+password+"\'}";
        }
}

创建    UserService

package com.example.demo.service;

import com.example.demo.entiy.UserBean;
import org.springframework.stereotype.Service;

@Service
public interface UserService {
    public UserBean getUserInfo();

    public String setUserInfo(String username, String password);
}

创建   UserServiceImpl

package com.example.demo.impl;
import com.example.demo.entiy.UserBean;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    public UserBean getUserInfo(){
        UserBean user = new UserBean();
        user.setName("jack");
        user.setPassword("12341234");
        return user;
    }

    @Override
    public String setUserInfo(String username, String password) {
        if(username == null || username.isEmpty()){
            return "用户名不可为空";
        }
        if(password == null || password.isEmpty()){
            return "密码不可为空";
        }
        return "登陆成功";
    }
}

创建   UserController  

package com.example.demo.control;

import com.example.demo.entiy.*;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping(value = "/getUserItem")
    public String getUserItem(){
        UserBean user = userService.getUserInfo();
        return user.toString();
    }

    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(HttpServletRequest request, String username, String password){
        String s = userService.setUserInfo(username, password);
        return s;
    }
}

请求地址:

       http://localhost:8080/getUserItem      

     http://localhost:8080/login  (post方式    参数:username,password)

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值