Java基于ssm框架的restful应用开发

好几年都没写过java的应用了,这里记录下使用java ssm框架、jwt如何进行rest应用开发,文中会涉及到全局异常拦截处理、jwt校验、token拦截器等内容。

1、jwt工具类

直接贴代码了,主要包括jwt的sign、verify、decode三个方法,具体实现如下:

package com.isoft.util;

import java.util.Date;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT; public class JwtUtil { private static final String SECRET = "xxxxx";
private static final String USERID = "userId";
private static final String ISSUER = "xxxx.com"; // 过期时间7天
private static final long EXPIRE_TIME = 7 * 24 * 60 * 60 * 1000; // jwt sign
public static String sign(Integer userId) {
try {
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
Algorithm algorithmHS = Algorithm.HMAC256(SECRET);
return JWT.create().withClaim(USERID, userId).withExpiresAt(date).withIssuer(ISSUER).sign(algorithmHS);
} catch (Exception e) {
return null;
}
} // jwt verify
public static boolean verify(String token, Integer userId) {
try {
Algorithm algorithmHS = Algorithm.HMAC256(SECRET);
JWTVerifier verifier = JWT.require(algorithmHS).withClaim(USERID, userId).withIssuer(ISSUER).build();
verifier.verify(token);
return true;
} catch (Exception e) {
System.out.println(e);
return false;
}
} // 返回token中的用户名
public static Integer getUserId(String token) {
try {
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim(USERID).asInt();
} catch (Exception e) {
return null;
}
}
}

2、全局异常处理类

ssm中进行rest全局异常拦截处理主要用到RestControllerAdvice型注解类,直接贴代码:

package com.isoft.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice; import com.isoft.po.Response; @RestControllerAdvice
public class ExceptionAdvice { /**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public Response handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
return new Response().failure("could not read json");
} /**
* 405 - Method Not Allowed
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Response handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
return new Response().failure("request method not supported");
} /**
* 500 - Internal Server Error
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public Response handleException(Exception e) {
System.err.println(e);
return new Response().failure(e.getMessage());
} }

这里主要拦截了405、400、500的n行yi'chang异常情况,可根据具体情况拓展。

3、自定义Response返回类

我们自定义的Response返回类格式如下:

{
"meta": {
"success": true,
"message": "ok"
},
data: Array or Object
}

代码如下:

package com.isoft.po;

public class Response {

	private static final String OK = "ok";
private static final String ERROR = "error"; private Meta meta;
private Object data; public Response success() {
this.meta = new Meta(true, OK);
return this;
} public Response success(Object data) {
this.meta = new Meta(true, OK);
this.data = data;
return this;
} public Response success(Object data, String msg){
this.meta = new Meta(true, msg);
this.data = data;
return this;
} public Response failure() {
this.meta = new Meta(false, ERROR);
return this;
} public Response failure(String message) {
this.meta = new Meta(false, message);
return this;
} public Meta getMeta() {
return meta;
} public Object getData() {
return data;
} public class Meta { private boolean success;
private String message; public Meta(boolean success) {
this.success = success;
} public Meta(boolean success, String message) {
this.success = success;
this.message = message;
} public boolean isSuccess() {
return success;
} public String getMessage() {
return message;
}
}
}

4、jwt的token拦截器Interceptor

spring mvc的Interceptor实现类一般是继承HandlerInterceptor接口并重写preHandle、postHandle、afterCompletion的方法来实现的,这里我们直接进行token的verify返回即可,具体代码如下:

package com.isoft.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import com.isoft.util.JwtUtil;
import com.isoft.util.StringUtil; /**
* jwt token拦截
* @author sd
*
*/
public class TokenInterceptor implements HandlerInterceptor{ @Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub } @Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub } @Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
String token = req.getHeader("Authorization");
if (StringUtil.isEmpty(token)) {
return false;
}
token = token.replace("Bearer ", "");
boolean isPass = JwtUtil.verify(token, JwtUtil.getUserId(token));
if (isPass) {
return true;
}
res.setStatus(401);
return false;
} }

写完这些还不行,需要将拦截器注册到spring-mvc的config中:

<!-- mvc拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/users/login"/>
<bean class="com.isoft.interceptor.TokenInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

这里使用mvc:exclude-mapping可以直接排除某个接口的拦截,比较方便。

5、mysql插入中文乱码解决

使用ssm框架mybatis进行数据插入时,发现插入中文进去后数据有乱码情况,除了设置数据库编码之外还解决不了问题的话,不妨看下mybatis的链接编码设置,如果是db.properties的话,使用如下设置:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf-8
jdbc.username=username
jdbc.password=password

6、一个简单的rest controller实例

package com.isoft.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import com.isoft.po.PageBean;
import com.isoft.po.Response;
import com.isoft.po.User;
import com.isoft.service.UserService;
import com.isoft.util.JwtUtil;
import com.isoft.util.StringUtil; /**
* 用户控制器类
*/ @RequestMapping("/users")
@RestController
public class UserController { @Resource
private UserService userService; /**
* 用户注册
*
* @param user
* @param res
* @return
* @throws Exception
*/
@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.CREATED)
public Response save(@RequestBody User user, HttpServletResponse res) throws Exception {
userService.add(user);
return new Response().success(user);
} /**
* 用户登录
*
* @param user
* @param res
* @return
* @throws Exception
*/
@RequestMapping(value = "login", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public Response login(@RequestBody User user, HttpServletResponse res) throws Exception {
System.out.println(user);
User u = userService.login(user);
if (u != null) {
String token = JwtUtil.sign(u.getId());
u.setToken(token);
u.setHashedPassword("");
return new Response().success(u, "login success");
}
return new Response().failure("username or password wrong");
} /**
* 用户查询带分页
*
* @param user
* @param res
* @return
* @throws Exception
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
public Response list(@RequestParam(value = "page", required = false, defaultValue = "1") String page,
@RequestParam(value = "rows", required = false, defaultValue = "10") String rows, User s_user, HttpServletResponse res)
throws Exception {
System.out.println(s_user);
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
Map<String, Object> map = new HashMap<String, Object>();
map.put("userName", StringUtil.formatLike(s_user.getUserName()));
map.put("email", s_user.getemail());
map.put("start", pageBean.getStart());
map.put("size", pageBean.getPageSize());
List<User> userList = userService.find(map);
return new Response().success(userList);
} /**
* 用户更新
*
* @param user
* @param res
* @return
* @throws Exception
*/
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.OK)
public Response update(@PathVariable Integer id, @RequestBody User user, HttpServletResponse res) throws Exception {
user.setId(id);
System.out.println(user);
Integer count = userService.update(user);
if (count != 0) {
return new Response().success();
}
return new Response().failure();
} /**
* 用户删除,支持批量
* @param id
* @param user
* @param res
* @return
* @throws Exception
*/
@RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public Response delete(@PathVariable String ids, HttpServletResponse res) throws Exception {
String[] idsStrings = ids.split(",");
for (String id : idsStrings) {
userService.delete(Integer.parseInt(id));
}
return new Response().success();
} /**
* 用户详情
* @param id
* @param user
* @param res
* @return
* @throws Exception
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
public Response getUser(@PathVariable Integer id, HttpServletResponse res) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
List<User> users = userService.find(map);
return new Response().success(users);
}
}

Java基于ssm框架的restful应用开发的更多相关文章

  1. 一款基于SSM框架技术的全栈Java web项目(已部署可直接体验)

    概述 此项目基于SSM框架技术的Java Web项目,是全栈项目,涉及前端.后端.插件.上线部署等各个板块,项目所有的代码都是自己编码所得,每一步.部分都有清晰的注释,完全不用担心代码混乱,可以轻松. ...

  2. 基于SSM框架的JavaWeb通用权限管理系统

    - - ->关注博主公众号[C you again],获取更多IT资源(IT技术文章,毕业设计.课程设计系统源码,经典游戏源码,HTML网页模板,PPT.简历模板,!!还可以投稿赚钱!!,点击查 ...

  3. 文献综述七:基于SSM的网上商城的开发与设计

    一.基本信息 标题:基于SSM的网上商城的开发与设计 时间:2018 出版源:Computer Knowledge and Technology 文件分类:对框架的研究 二.研究背景 为了解决现在电商 ...

  4. Java基于SSM在线学习系统设计与实现

                 Spring+SpringMVC+MyBatis+Bootstrap+Vue开发在线学习系统 本课题的主要内容是开发基于Java EE的在线学习平台,使用MVC经典开发模式. ...

  5. 基于SSM框架贺州学院校园二手交易平台设计与实现

    前言 这个是我当时的毕业论文,分享出来,给同学们参考. 绪论 随着中国新四大发明的诞生,网购成了千千万万网友们购物的新方式,新的购物方式促进商业的发展,但随着人们生活水平的提高,许多新购置的物品用了没 ...

  6. JAVA:ssm框架搭建

    文章来源:http://www.cnblogs.com/hello-tl/p/8328071.html 环境简介 : jdk1.7.0_25/jdk1.8.0_31  tomcat-7.0.81  m ...

  7. spring boot 简介(基于SSM框架的一个升级版本吧)

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...

  8. 基于SSM框架配置多数据源

    项目基于ssm + maven,通过注解可以实现自动切换数据源. 一.pom.xml <?xml version="1.0" encoding="UTF-8&quo ...

  9. pageoffice实现网页打开编辑保存word文档(基于SSM框架)

    pageoffice是一款网页集成word.excel...等office工具 并不免费,但可以试用练习 SSM框架搭建过程就省略了 注意:由于谷歌/火狐升级,不支持插件嵌入,建议使用POBrowse ...

随机推荐

  1. WPF添加样式字典Style

    新建Resource Dictionary文件,取名Style: 将常用的样式写入该文件: 在App.xaml中引用该文件: <Application x:Class="Machine ...

  2. Mysql 练习题一

    库操作: 1. 创建 数据库  create database db1; 2. 使用数据库 use db1 3. 查看表  show tables; 4. 删除  drop database db1  ...

  3. python+pcap+dpkt 抓包小实例

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ 网络数据包捕获与分析程序 """ imp ...

  4. django 使用其自带的验证系统 进行用户名有效性验证 登录状态验证 登入操作 登出操作

    from django.shortcuts import render, redirect from django.contrib.auth import authenticate, login, l ...

  5. 极验(Geetest) Laravel 5 集成开发包,让验证更安全

    简述 在网站开发中使用频率最高的工具之一便是验证码,验证码在此也是多种多样,不过简单的图片验证码已经可以被机器识别,极验验证码提供了一个安全可靠的滑动验证码体系,让网站开发更加安全. 先感受一下这种验 ...

  6. (3)Oracle基础--表

    · 认识表 Oracle中的表都是存储在表空间中,具有以下特点:  <1> 数据的基本存储单元  <2> 二维结构 行:又称为‘记录’ 列:又称为‘字段或域’  <3&g ...

  7. C语言的组成 以及预编译

    这么多年过去了,回头再来学习一下C语言,发现很多不一样的感觉 #include <stdio.h> int main(int argc, const char * argv[]) { pr ...

  8. collectd+influxdb+grafana

    今天一天都在弄这个,最终发现在配置grafana的时候选择influxdb的版本时候选错了.(挠头~~~!!!) collectd的配置还算简单,基本看过配置文件就比较清楚. influxdb(Go ...

  9. Class 和 MetaClass

    在 OC 中,类的一个实例定义如下: /// Represents an instance of a class. struct objc_object { Class _Nonnull isa OB ...

  10. Java爬虫项目实战(一)

    目的: 通过网络爬虫爬取中国最小粒度的区域维度信息,包括省(Province) .市(City).县(County).镇(town).村委会(village) 主网站链接: http://www.st ...