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

第一步:创建项目

 打开 idea 

        File -->  new -->project -->Spring Initalizr -->next      选择Spring web 然后finish创建项目记录(二):创建Spring 项目接口实现

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

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

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

 第三步:写接口文件

创建实体类  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)

上一篇:C#中绝对路径获取以及获取上上级目录


下一篇:模拟BeanUtils.populate(Object bean, Map properties)的简单实现