Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
mall
Commits
eca02f24
Commit
eca02f24
authored
Apr 13, 2019
by
macro
Browse files
demo通用返回结果改造
parent
cfa9871b
Changes
4
Hide whitespace changes
Inline
Side-by-side
mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java
View file @
eca02f24
...
...
@@ -69,7 +69,7 @@ public class Swagger2Config {
.
build
();
}
List
<
SecurityReference
>
defaultAuth
()
{
private
List
<
SecurityReference
>
defaultAuth
()
{
List
<
SecurityReference
>
result
=
new
ArrayList
<>();
AuthorizationScope
authorizationScope
=
new
AuthorizationScope
(
"global"
,
"accessEverything"
);
AuthorizationScope
[]
authorizationScopes
=
new
AuthorizationScope
[
1
];
...
...
mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java
View file @
eca02f24
package
com.macro.mall.demo.controller
;
import
com.macro.mall.demo.dto.CommonPage
;
import
com.macro.mall.demo.dto.CommonResult
;
import
com.macro.mall.demo.dto.PmsBrandDto
;
import
com.macro.mall.demo.service.DemoService
;
import
com.macro.mall.model.PmsBrand
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.slf4j.Logger
;
...
...
@@ -14,6 +16,8 @@ import org.springframework.validation.BindingResult;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
/**
* 测试controller
*/
...
...
@@ -28,24 +32,24 @@ public class DemoController {
@ApiOperation
(
value
=
"获取全部品牌列表"
)
@RequestMapping
(
value
=
"/brand/listAll"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Object
getBrandList
()
{
return
new
CommonResult
()
.
success
(
demoService
.
listAllBrand
());
public
CommonResult
<
List
<
PmsBrand
>>
getBrandList
()
{
return
CommonResult
.
success
(
demoService
.
listAllBrand
());
}
@ApiOperation
(
value
=
"添加品牌"
)
@RequestMapping
(
value
=
"/brand/create"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
Objec
t
createBrand
(
@Validated
@RequestBody
PmsBrandDto
pmsBrand
,
BindingResult
result
)
{
public
CommonResul
t
createBrand
(
@Validated
@RequestBody
PmsBrandDto
pmsBrand
,
BindingResult
result
)
{
if
(
result
.
hasErrors
())
{
return
new
CommonResult
()
.
validateFailed
(
result
.
getFieldError
().
getDefaultMessage
());
return
CommonResult
.
validateFailed
(
result
.
getFieldError
().
getDefaultMessage
());
}
CommonResult
commonResult
;
int
count
=
demoService
.
createBrand
(
pmsBrand
);
if
(
count
==
1
)
{
commonResult
=
new
CommonResult
()
.
success
(
pmsBrand
);
commonResult
=
CommonResult
.
success
(
pmsBrand
);
LOGGER
.
debug
(
"createBrand success:{}"
,
pmsBrand
);
}
else
{
commonResult
=
new
CommonResult
()
.
failed
();
commonResult
=
CommonResult
.
failed
(
"操作失败"
);
LOGGER
.
debug
(
"createBrand failed:{}"
,
pmsBrand
);
}
return
commonResult
;
...
...
@@ -54,17 +58,17 @@ public class DemoController {
@ApiOperation
(
value
=
"更新品牌"
)
@RequestMapping
(
value
=
"/brand/update/{id}"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
Objec
t
updateBrand
(
@PathVariable
(
"id"
)
Long
id
,
@Validated
@RequestBody
PmsBrandDto
pmsBrandDto
,
BindingResult
result
)
{
public
CommonResul
t
updateBrand
(
@PathVariable
(
"id"
)
Long
id
,
@Validated
@RequestBody
PmsBrandDto
pmsBrandDto
,
BindingResult
result
)
{
if
(
result
.
hasErrors
()){
return
new
CommonResult
()
.
validateFailed
(
result
.
getFieldError
().
getDefaultMessage
());
return
CommonResult
.
validateFailed
(
result
.
getFieldError
().
getDefaultMessage
());
}
CommonResult
commonResult
;
int
count
=
demoService
.
updateBrand
(
id
,
pmsBrandDto
);
if
(
count
==
1
)
{
commonResult
=
new
CommonResult
()
.
success
(
pmsBrandDto
);
commonResult
=
CommonResult
.
success
(
pmsBrandDto
);
LOGGER
.
debug
(
"updateBrand success:{}"
,
pmsBrandDto
);
}
else
{
commonResult
=
new
CommonResult
()
.
failed
();
commonResult
=
CommonResult
.
failed
(
"操作失败"
);
LOGGER
.
debug
(
"updateBrand failed:{}"
,
pmsBrandDto
);
}
return
commonResult
;
...
...
@@ -73,29 +77,30 @@ public class DemoController {
@ApiOperation
(
value
=
"删除品牌"
)
@RequestMapping
(
value
=
"/brand/delete/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Objec
t
deleteBrand
(
@PathVariable
(
"id"
)
Long
id
)
{
public
CommonResul
t
deleteBrand
(
@PathVariable
(
"id"
)
Long
id
)
{
int
count
=
demoService
.
deleteBrand
(
id
);
if
(
count
==
1
)
{
LOGGER
.
debug
(
"deleteBrand success :id={}"
,
id
);
return
new
CommonResult
()
.
success
(
null
);
return
CommonResult
.
success
(
null
);
}
else
{
LOGGER
.
debug
(
"deleteBrand failed :id={}"
,
id
);
return
new
CommonResult
()
.
failed
();
return
CommonResult
.
failed
(
"操作失败"
);
}
}
@ApiOperation
(
value
=
"分页获取品牌列表"
)
@RequestMapping
(
value
=
"/brand/list"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Object
listBrand
(
@RequestParam
(
value
=
"pageNum"
,
defaultValue
=
"1"
)
Integer
pageNum
,
@RequestParam
(
value
=
"pageSize"
,
defaultValue
=
"3"
)
Integer
pageSize
)
{
return
new
CommonResult
().
pageSuccess
(
demoService
.
listBrand
(
pageNum
,
pageSize
));
public
CommonResult
<
CommonPage
<
PmsBrand
>>
listBrand
(
@RequestParam
(
value
=
"pageNum"
,
defaultValue
=
"1"
)
Integer
pageNum
,
@RequestParam
(
value
=
"pageSize"
,
defaultValue
=
"3"
)
Integer
pageSize
)
{
List
<
PmsBrand
>
brandList
=
demoService
.
listBrand
(
pageNum
,
pageSize
);
return
CommonResult
.
success
(
CommonPage
.
restPage
(
brandList
));
}
@ApiOperation
(
value
=
"根据编号查询品牌信息"
)
@RequestMapping
(
value
=
"/brand/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Object
brand
(
@PathVariable
(
"id"
)
Long
id
)
{
return
new
CommonResult
()
.
success
(
demoService
.
getBrand
(
id
));
public
CommonResult
<
PmsBrand
>
brand
(
@PathVariable
(
"id"
)
Long
id
)
{
return
CommonResult
.
success
(
demoService
.
getBrand
(
id
));
}
}
mall-demo/src/main/java/com/macro/mall/demo/dto/CommonPage.java
0 → 100644
View file @
eca02f24
package
com.macro.mall.demo.dto
;
import
com.github.pagehelper.PageInfo
;
import
java.util.List
;
/**
* 分页数据封装类
*/
public
class
CommonPage
<
T
>
{
private
Integer
pageNum
;
private
Integer
pageSize
;
private
Long
totalPage
;
private
List
<
T
>
list
;
/**
* 将PageHelper分页后的list转为分页信息
*/
public
static
<
T
>
CommonPage
<
T
>
restPage
(
List
<
T
>
list
)
{
CommonPage
<
T
>
result
=
new
CommonPage
<>();
PageInfo
<
T
>
pageInfo
=
new
PageInfo
<>(
list
);
result
.
setTotalPage
(
pageInfo
.
getTotal
()
/
pageInfo
.
getPageSize
());
result
.
setPageNum
(
pageInfo
.
getPageNum
());
result
.
setPageSize
(
pageInfo
.
getPageSize
());
result
.
setList
(
pageInfo
.
getList
());
return
result
;
}
public
Integer
getPageNum
()
{
return
pageNum
;
}
public
void
setPageNum
(
Integer
pageNum
)
{
this
.
pageNum
=
pageNum
;
}
public
Integer
getPageSize
()
{
return
pageSize
;
}
public
void
setPageSize
(
Integer
pageSize
)
{
this
.
pageSize
=
pageSize
;
}
public
Long
getTotalPage
()
{
return
totalPage
;
}
public
void
setTotalPage
(
Long
totalPage
)
{
this
.
totalPage
=
totalPage
;
}
public
List
<
T
>
getList
()
{
return
list
;
}
public
void
setList
(
List
<
T
>
list
)
{
this
.
list
=
list
;
}
}
mall-demo/src/main/java/com/macro/mall/demo/dto/CommonResult.java
View file @
eca02f24
package
com.macro.mall.demo.dto
;
import
com.github.pagehelper.PageInfo
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* 通用返回对象
*/
public
class
CommonResult
{
public
class
CommonResult
<
T
>
{
public
static
final
int
SUCCESS
=
0
;
public
static
final
int
FAILED
=
1
;
public
static
final
int
VALIDATE_FAILED
=
2
;
private
int
code
;
private
String
message
;
private
Object
data
;
private
T
data
;
/**
* 普通成功返回
*
* @param data 获取的数据
*/
public
CommonResult
success
(
Object
data
)
{
this
.
code
=
SUCCESS
;
this
.
message
=
"操作成功"
;
this
.
data
=
data
;
return
this
;
}
/**
* 返回分页成功数据
*/
public
CommonResult
pageSuccess
(
List
data
)
{
PageInfo
pageInfo
=
new
PageInfo
(
data
);
long
totalPage
=
pageInfo
.
getTotal
()
/
pageInfo
.
getPageSize
();
Map
<
String
,
Object
>
result
=
new
HashMap
<>();
result
.
put
(
"pageSize"
,
pageInfo
.
getPageSize
());
result
.
put
(
"totalPage"
,
totalPage
);
result
.
put
(
"pageNum"
,
pageInfo
.
getPageNum
());
result
.
put
(
"list"
,
pageInfo
.
getList
());
this
.
code
=
SUCCESS
;
this
.
message
=
"操作成功"
;
this
.
data
=
result
;
return
this
;
public
static
<
T
>
CommonResult
<
T
>
success
(
T
data
)
{
CommonResult
<
T
>
result
=
new
CommonResult
<
T
>();
result
.
setCode
(
SUCCESS
);
result
.
setData
(
data
);
return
result
;
}
/**
* 普通失败提示信息
*/
public
CommonResult
failed
()
{
this
.
code
=
FAILED
;
this
.
message
=
"操作失败"
;
return
this
;
public
static
<
T
>
CommonResult
<
T
>
failed
(
String
message
)
{
CommonResult
result
=
new
CommonResult
();
result
.
setCode
(
FAILED
);
result
.
setMessage
(
message
);
return
result
;
}
/**
...
...
@@ -60,10 +38,11 @@ public class CommonResult {
*
* @param message 错误信息
*/
public
CommonResult
validateFailed
(
String
message
)
{
this
.
code
=
VALIDATE_FAILED
;
this
.
message
=
message
;
return
this
;
public
static
<
T
>
CommonResult
<
T
>
validateFailed
(
String
message
)
{
CommonResult
result
=
new
CommonResult
();
result
.
setCode
(
VALIDATE_FAILED
);
result
.
setMessage
(
message
);
return
result
;
}
public
int
getCode
()
{
...
...
@@ -82,11 +61,11 @@ public class CommonResult {
this
.
message
=
message
;
}
public
Object
getData
()
{
public
T
getData
()
{
return
data
;
}
public
void
setData
(
Object
data
)
{
public
void
setData
(
T
data
)
{
this
.
data
=
data
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment