> Java 登陆API开发 - Yuyy
Yuyy
Yuyy
Java 登陆API开发

Github

https://github.com/EchoGroot/fourth_spring_simfyspider.git

知识

REST,即Representational State Transfer,表述行状态转移 Restful架构就是符合Rest原则的软件架构
简单来说,通过网络,将其中的资源(文本、图片、歌曲、服务)的状态改变
具体来说,就是HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。
它们分别对应四种基本操作:
GET用来获取资源,
POST用来新建资源(也可以用于更新资源),
PUT用来更新资源,
DELETE用来删除资源。

JSON : JavaScript Object Notation 即JS的对象标记法
JS中用字符串表示对象的表示法
现演变为传输协议和文件格式,App通常用JSON格式与服务器通信

将对象转为JSON的注解
@ResponseBody : 标记将某个方法的返回值自动转换为JSON字符串返回
@RestController : 代替@Controller注解,将整个Controller的所有方法的返回值自动转换为JSON返回

辅助对象:
Form对象 : 封装用户请求参数的Java对象
Result对象 : 服务端返回给外部的统一请求格式,具有如下属性
success字段:表示操作是否成功
code字段:操作结果码,自行约定。比如,0000表示成功,1001表示用户不存在
message字段:操作结果信息,文字说明本次操作的结果,
data字段:本次操作携带的数据,比如某次查询请求带回的结果列表

思路

开发Controller
创建请求接口
使用Form接收参数
调用service处理登录业务
封装返回结果成JSON
开发Service
调用DAO处理数据
开发DAL
开发模型类
开发Repository

运行

http://yuyy.info/wp-content/uploads/2019/04/Snipaste_2019-04-30_09-11-19.png

代码

LoginController

package com.fourth.spring.simfyspider.web;

import com.fourth.spring.simfyspider.common.Result;
import com.fourth.spring.simfyspider.service.LoginService;
import com.fourth.spring.simfyspider.web.form.LoginForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("login")
    public Result login(LoginForm form){
        System.out.println(form);
        Boolean loginSuccess=loginService.login(form);

        return loginSuccess?Result.success():Result.fail("00000","登陆失败");
    }
}

LoginService

package com.fourth.spring.simfyspider.service;

import com.fourth.spring.simfyspider.web.form.LoginForm;

public interface LoginService {
    Boolean login(LoginForm form);
}

LoginServiceImpl

package com.fourth.spring.simfyspider.service.impl;

import com.fourth.spring.simfyspider.dal.UserRepository;
import com.fourth.spring.simfyspider.domain.User;
import com.fourth.spring.simfyspider.service.LoginService;
import com.fourth.spring.simfyspider.web.form.LoginForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public Boolean login(LoginForm form) {
        User user=new User();
        user.setUserName(form.getUsrname());
        user.setUserPassword(form.getPassword());
        Example<User> userExample = Example.of(user);
        Optional<User> result=userRepository.findOne(userExample);
        return result.isPresent();
    }
}

User

package com.fourth.spring.simfyspider.domain;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "chinasoft_class_5_login")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;

    private String userName;

    private String userPassword;

    private Date lastLoginDate;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public Date getLastLoginDate() {
        return lastLoginDate;
    }

    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                ", lastLoginDate=" + lastLoginDate +
                '}';
    }
}

UserRepository

package com.fourth.spring.simfyspider.dal;

import com.fourth.spring.simfyspider.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User,Integer> {
}

Result

package com.fourth.spring.simfyspider.common;

public class Result {
    private boolean success;
    private String code;
    private String message;
    private Object data;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public Result(){};

    public Result(Boolean success){
        this.success=success;
        this.code=success?"10000":"00000";
        this.message=success?"执行成功":"执行失败";
    }

    public Result(Boolean success,String code,String message){
       this.success=success;
       this.code=code;
       this.message=message;
    }

    public Result(Boolean success,String code,String message,Object data){
        this.success=success;
        this.code=code;
        this.message=message;
        this.data=data;
    }

    public static Result success(){
        return success(null);
    }

    public static Result success(Object data){
        return new Result(true,"10000","执行成功",data);
    }

    public static Result fail(String code,String message){
        return new Result(false,code,message,null);
    }

    public static Result generalFail(){
        return fail("00000","未知错误");
    }
}

发表评论

textsms
account_circle
email

Yuyy

Java 登陆API开发
Github https://github.com/EchoGroot/fourth_spring_simfyspider.git 知识 REST,即Representational State Transfer,表述行状态转移 Restful架构就是符合Rest原则的软件架构 …
扫描二维码继续阅读
2019-04-30
友情链接
标签
归档
近期文章
分类
近期文章