Awesome Open Source
Awesome Open Source

SSMshiroswagger


1

webHTTP// webwebapp


2

AJAXjsonXMLVueReactAngularJquery


3RESTful

RESTRepresentational State Transfer""""Resources""RESTURLHTTPHTTPgetposthttp

GETSELECT
POSTCREATE
PUTUPDATE
DELETEDELETE
HEAD
OPTIONS

4RESTful RESTful API

GET /zoos
POST /zoos
GET /zoos/ID
PUT /zoos/ID
PATCH /zoos/ID
DELETE /zoos/ID
GET /zoos/ID/animals
DELETE /zoos/ID/animals/ID

4

RESTJSONRESTJSONJSONJSON

{
  "code": 200,
  "message": "success",
  "data": {}
}
{
  "code": 200,
  "message": "",
  "data": {
    "user": {
      "id": 1,
      "account": "test_user",
      "password": "123456"
          }
  }
}

JAVA

public class Msg {

	private int code;

	private String message;

	private Map<String,Object> data = new HashMap<String, Object>();

	private Msg(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public static Msg message(int code,String message){
		Msg result = new Msg(code,message);
		return result;
	}
	/**
	 * 
	 * @return
	 */
	public static Msg success(){
		Msg result = new Msg(200,"");
		return result;
	}

	public static Msg success(String message){
		Msg result = new Msg(200,message);
		return result;
	}

	public static Msg error(){
		Msg result = new Msg(400,"");
		return result;
	}
	public static Msg error(String message){
		Msg result = new Msg(400,message);
		return result;
	}

	/**
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public Msg add(String key,Object value){
		this.data.put(key,value);
		return this;
	}

	public int getCode() {
		return code;
	}

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

	public String getMessage() {
		return message;
	}

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

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

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

MsgdatadataHashMapadd

[email protected]@RequestBodyjsonJAVA @[email protected]@ResponseBody


5

try [email protected]

/**
 *
 * ,,try catch
 *
 * Copyright(C) 2018-2018
 * Author:wanhaoran
 * Date: 2018/6/1 8:37
 */
@RestControllerAdvice
public class ExceptionAdvice {

	private static final Logger LOGGER = LogManager.getLogger(UserRealm.class);
	/**
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(HttpMessageNotReadableException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public Msg handleHttpMessageNotReadableException(Exception e){
		e.printStackTrace();
		return Msg.message(400,"");
	}

	/**
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public Msg handleMethodArgumentNotValidException(Exception e){
		return Msg.message(400,"");
	}

	/**
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(IException.class)
	@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
	public Msg handleIException(IException e){
		return Msg.message(417,"");
	}

	/**
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(ArithmeticException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public Msg handleArithmeticException(ArithmeticException e){
		return Msg.message(500,"");
	}

	/**
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(AuthenticationException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public Msg handleAuthenticationException(AuthenticationException e){
		LOGGER.error(e);
		return Msg.message(401,"");
	}

	@ExceptionHandler(UnknownAccountException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public Msg handleUnknownAccountException(UnknownAccountException e){
		LOGGER.error(e);
		return Msg.message(401,"");
	}


	/**
	 * shiro
	 * @param e
	 * @return
	 */
	@ExceptionHandler(UnauthorizedException.class)
	@ResponseStatus(HttpStatus.FORBIDDEN)
	public Msg handleUnauthorizedException(UnauthorizedException e){
		return Msg.message(403,"");
	}
}

@[email protected]@ResponseStatusHTTPMsg @[email protected])Exception

/**
 *
 * 
 *
 * Copyright(C) 2018-2018
 * Author: wanhaoran
 * Date: 2018/6/1 8:29
 */
public class IException extends RuntimeException{
	private static final long serialVersionUID = 7144771828212718116L;
	private String message;

	public IException(String message){
		this.message = message;
	}

	@Override
	public String getMessage() {
		return message;
	}

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

ControllerAdvice


6

  @RequiresPermissions("user:add")
	@ApiOperation(value = "", notes = "")
	@PostMapping()
	public Msg addUser(@RequestBody @Valid SysUser sysUser) throws Exception{
		sysUserService.addUser(sysUser);
		return Msg.message(201,"");
	}

SysUserHibernate [email protected]

@NotEmpty(message = "")
private String account;

@Length(min = 6,message = "6")
private String password;
	/**
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public Msg handleMethodArgumentNotValidException(Exception e){
		return Msg.message(400,"");
	}
{
"code": 400,
"message": "",
"data": {}
}

7

springswaggerswaggerControllerswagger swagger-configswagger

**
 *
 * Swagger2
 *
 * Copyright(C) 2018-2018
 * Author: wanhaoran
 * Date: 2018/5/31 16:05
 */
//@Configuration //SSM spring boot  spring 
@EnableWebMvc // spring boot SSM 
@EnableSwagger2
public class SwaggerConfig {
	@Bean
	public Docket buildDocket(){
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(buildApiInfo())
				.select().apis(RequestHandlerSelectors.basePackage("controller"))// controller
				.paths(PathSelectors.any())
				.build();
	}

	//  API 
	private ApiInfo buildApiInfo(){
		return new ApiInfoBuilder()
				.title("API")
				.termsOfServiceUrl("")
				.description("Spring MVCSwagger2Restful API")
				.build();

	}
}

springmvc.xml <beanclass="common.SwaggerConfig"/> swagger

swagger

8shiro

shiro

shirogoogleshiro Spring SecuritySpring SecurityShiro

shiro

shiroshiro shiroshiroshiroorg.apache.shiro.cache.MemoryConstrainedCacheManagerEhCacheorg.apache.shiro.cache.ehcache.EhCacheManager redisshiro


  • email
  • star

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (697,184
Rest Api (8,338
Redis (6,829
Rest (4,145
Jwt (3,647
Swagger (2,685
Restful (1,246
Ssm (416
Shiro (395
Management System (385
Swagger2 (255
Shiro Redis (10
Related Projects