- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring MVC+FastJson+Swagger集成的完整实例教程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
基础部分 。
1. fastjson 简介 。
fastjson是一个java库,可用于将java对象转换为json表示。它也可以被用来将一个json字符串转换成一个等效的java对象。在转换速度上应该是最快的,几乎成为了项目的标配(在ajax请求和接口开发时一般都会用fastjson而不再使用jackson).
github: https://github.com/alibaba/fastjson 。
特性:
主要特点:
2. fastjson api 。
fastjson api入口类是com.alibaba.fastjson.json,常用的序列化操作都可以在json类上的静态方法直接完成.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// 把json文本parse为jsonobject或者jsonarray
public
static
final
object parse(string text);
// 把json文本parse成jsonobject
public
static
final
jsonobject parseobject(string text);
// 把json文本parse为javabean
public
static
final
<t> t parseobject(string text,
class
<t> clazz);
// 把json文本parse成jsonarray
public
static
final
jsonarray parsearray(string text);
// 把json文本parse成javabean集合
public
static
final
<t> list<t> parsearray(string text,
class
<t> clazz);
// 将javabean序列化为json文本
public
static
final
string tojsonstring(object object);
// 将javabean序列化为带格式的json文本
public
static
final
string tojsonstring(object object,
boolean
prettyformat);
// 将javabean转换为jsonobject或者jsonarray
public
static
final
object tojson(object javaobject);
|
jsonarray:相当于list<object> 。
jsonobject:相当于map<string, object> 。
serializeconfig: 是对序列化过程中一些序列化过程的特殊配置, 如对一些字段进行格式处理(日期、枚举等) 。
serializewriter:相当于stringbuffer 。
serializerfeature属性 :
实战部分 。
1、pom.xml 中引入spring mvc、 fastjson 依赖 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
<project xmlns=
"http://maven.apache.org/pom/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation=
"http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelversion>
4.0
.
0
</modelversion>
<groupid>com.mengdee</groupid>
<artifactid>platform-springmvc-webapp</artifactid>
<packaging>war</packaging>
<version>
0.0
.
1
-snapshot</version>
<name>platform-springmvc-webapp maven webapp</name>
<url>http:
//maven.apache.org</url>
<properties>
<project.build.sourceencoding>utf-
8
</project.build.sourceencoding>
<junit.version>
3.8
.
1
</junit.version>
<log4j.version>
2.5
</log4j.version>
<jstl.version>
1.2
</jstl.version>
<spring.version>
4.2
.
3
.release</spring.version>
<fastjson.version>
1.2
.
32
</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>
3.8
.
1
</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>jstl</artifactid>
<version>${jstl.version}</version>
</dependency>
<!-- springframework -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context-support</artifactid>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-jdbc</artifactid>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupid>com.alibaba</groupid>
<artifactid>fastjson</artifactid>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
<!-- 使用aliyun镜像 -->
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http:
//maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories>
<build>
<finalname>platform-springmvc-webapp</finalname>
</build>
</project>
|
2、 配置web.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<!doctype web-app
public
"-//sun microsystems, inc.//dtd web application 2.3//en"
"http://java.sun.com/dtd/web-app_2_3.dtd"
>
<web-app>
<display-name>archetype created web application</display-name>
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:conf/spring/spring-*.xml</param-value>
</context-param>
<listener>
<description>spring监听器</description>
<listener-
class
>org.springframework.web.context.contextloaderlistener</listener-
class
>
</listener>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-
class
>org.springframework.web.servlet.dispatcherservlet</servlet-
class
>
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterencodingfilter</filter-name>
<filter-
class
>org.springframework.web.filter.characterencodingfilter</filter-
class
>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-
8
</param-value>
</init-param>
<init-param>
<param-name>forceencoding</param-name>
<param-value>
true
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterencodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>
404
</error-code>
<location>/index.jsp</location>
</error-page>
</web-app>
|
3、 配置spring-servlet.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/xmlschema-instance"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xsi:schemalocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-4.2.xsd
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-
package
=
"com.mengdee.manage.controller"
/>
<bean id=
"viewresolver"
class
=
"org.springframework.web.servlet.view.internalresourceviewresolver"
>
<property name=
"prefix"
value=
"/"
></property> <!-- 视图文件的前缀 -->
<property name=
"suffix"
value=
".jsp"
></property> <!-- 视图文件的后缀名 -->
<!-- view是用什么显示,这里是jsp,还可以用velocity之类的 -->
<property name=
"viewclass"
value=
"org.springframework.web.servlet.view.jstlview"
></property>
</bean>
<!-- 启动spring mvc的注解功能,完成请求和注解pojo的映射 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults=
"true"
>
<!-- 配置fastjson 替换原来的jackson支持 -->
<bean
class
=
"com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverter"
>
<property name=
"supportedmediatypes"
>
<list>
<value>text/html;charset=utf-
8
</value>
<value>application/json</value>
</list>
</property>
<property name=
"features"
>
<list>
<value>quotefieldnames</value> <!-- 输出key时是否使用双引号,默认为
true
-->
<value>writemapnullvalue</value> <!-- 是否输出值为
null
的字段,默认为
false
-->
<!--
<value>disablecircularreferencedetect</value>
<value>writedateusedateformat</value>
<value>writenullstringasempty</value> 字符类型字段如果为
null
,输出为
""
,而非
null
<value>writenullnumberaszero</value> 数值字段如果为
null
,输出为
0
,而非
null
<value>writenullbooleanasfalse</value>
boolean
字段如果为
null
,输出为
false
,而非
null
<value>writenulllistasempty</value> list字段如果为
null
,输出为[],而非
null
-->
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
|
4、java 。
education:学历(枚举类) 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package
com.mengdee.manage.entity;
import
java.util.hashmap;
import
java.util.map;
/**
* 学历
* @author administrator
*
*/
public
enum
education {
kindergarten(
"幼儿园"
,
1
),
elementary(
"小学"
,
2
),
junior_middle(
"初级中学"
,
3
),
senior_middle(
"高级中学"
,
4
),
university(
"大学"
,
5
),
college(
"学院"
,
6
);
private
static
final
map<integer, education> education_map =
new
hashmap<integer, education>();
static
{
for
(education education : education.values()) {
education_map.put(education.getindex(), education);
}
}
private
string text;
private
int
index;
private
education(string text,
int
index) {
this
.text = text;
this
.index = index;
}
public
string gettext() {
return
text;
}
public
void
settext(string text) {
this
.text = text;
}
public
int
getindex() {
return
index;
}
public
void
setindex(
int
index) {
this
.index = index;
}
public
static
education getenum(integer index) {
return
education_map.get(index);
}
}
|
person
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package
com.mengdee.manage.entity;
import
java.util.arraylist;
import
java.util.date;
import
java.util.list;
import
java.util.map;
import
com.alibaba.fastjson.annotation.jsonfield;
public
class
person {
private
long
id;
private
string name;
private
byte
gender;
// 性别 1:男 2:女
private
short
age;
// 年龄
private
long
salary;
// 薪水
private
double
weight;
// 体重
private
char
level;
// 评级
private
boolean
adult;
// 是否成年人
private
date birthday;
// 生日
private
education education;
// 学历
private
string[] hobbies;
// 爱好
private
list<dog> dogs;
// 宠物狗
private
map<string, object> address;
// 住址
// 使用注解控制是否要序列化
@jsonfield
(serialize =
false
)
private
list<object> obj =
new
arraylist<>();
public
person() {
}
public
person(
long
id, string name,
byte
gender,
short
age,
long
salary,
double
weight,
char
level,
boolean
adult,
date birthday, string[] hobbies, list<dog> dogs, map<string, object> address) {
super
();
this
.id = id;
this
.name = name;
this
.gender = gender;
this
.age = age;
this
.salary = salary;
this
.weight = weight;
this
.level = level;
this
.adult = adult;
this
.birthday = birthday;
this
.hobbies = hobbies;
this
.dogs = dogs;
this
.address = address;
}
public
long
getid() {
return
id;
}
public
void
setid(
long
id) {
this
.id = id;
}
public
string getname() {
return
name;
}
public
void
setname(string name) {
this
.name = name;
}
public
byte
getgender() {
return
gender;
}
public
void
setgender(
byte
gender) {
this
.gender = gender;
}
public
short
getage() {
return
age;
}
public
void
setage(
short
age) {
this
.age = age;
}
public
long
getsalary() {
return
salary;
}
public
void
setsalary(
long
salary) {
this
.salary = salary;
}
public
double
getweight() {
return
weight;
}
public
void
setweight(
double
weight) {
this
.weight = weight;
}
public
char
getlevel() {
return
level;
}
public
void
setlevel(
char
level) {
this
.level = level;
}
public
boolean
isadult() {
return
adult;
}
public
void
setadult(
boolean
adult) {
this
.adult = adult;
}
public
date getbirthday() {
return
birthday;
}
public
void
setbirthday(date birthday) {
this
.birthday = birthday;
}
// 处理序列化枚举类型,默认的值是序列化枚举值字符串,而不是枚举绑定的索引或者文本
@jsonfield
(name =
"edu"
)
public
int
getedu(){
return
education.getindex();
}
@jsonfield
(name =
"edu"
)
public
void
setedu(
int
index){
this
.education = education.getenum(index);
}
@jsonfield
(serialize =
false
)
public
education geteducation() {
return
education;
}
@jsonfield
(serialize =
false
)
public
void
seteducation(education education) {
this
.education = education;
}
public
string[] gethobbies() {
return
hobbies;
}
public
void
sethobbies(string[] hobbies) {
this
.hobbies = hobbies;
}
public
list<dog> getdogs() {
return
dogs;
}
public
void
setdogs(list<dog> dogs) {
this
.dogs = dogs;
}
public
map<string, object> getaddress() {
return
address;
}
public
void
setaddress(map<string, object> address) {
this
.address = address;
}
}
|
testcontroller 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
package
com.mengdee.manage.controller;
import
java.text.decimalformat;
import
java.util.arraylist;
import
java.util.date;
import
java.util.hashmap;
import
java.util.list;
import
java.util.map;
import
org.springframework.stereotype.controller;
import
org.springframework.web.bind.annotation.requestmapping;
import
org.springframework.web.bind.annotation.responsebody;
import
com.alibaba.fastjson.json;
import
com.alibaba.fastjson.jsonarray;
import
com.alibaba.fastjson.serializer.doubleserializer;
import
com.alibaba.fastjson.serializer.serializeconfig;
import
com.alibaba.fastjson.serializer.simpledateformatserializer;
import
com.mengdee.manage.entity.address;
import
com.mengdee.manage.entity.dog;
import
com.mengdee.manage.entity.education;
import
com.mengdee.manage.entity.person;
@controller
public
class
testcontroller {
private
static
serializeconfig serializeconfig =
new
serializeconfig();
static
{
serializeconfig.put(date.
class
,
new
simpledateformatserializer(
"yyyy-mm-dd hh:mm:ss"
));
serializeconfig.put(
double
.
class
,
new
doubleserializer(
new
decimalformat(
"0.00"
)));
}
@requestmapping
(
"/index"
)
public
string index(){
return
"index"
;
}
// javabean to object
@requestmapping
(
"/json"
)
@responsebody
public
object json(){
person person =
new
person();
person.setid(1l);
person.setname(
"mengdee"
);
person.setage((
short
)
18
);
//
/*
{
"birthday": null,
"weight": 0,
"dogs": null,
"adult": false,
"hobbies": null,
"education": null,
"id": 1,
"level": "",
"address": null,
"age": 18,
"name": "mengdee",
"gender": 0,
"salary": 0
}
*/
object personjson = json.tojson(person);
return
personjson;
}
// javabean to string
@requestmapping
(
"/json2"
)
@responsebody
public
string json2(){
person person =
new
person();
person.setid(1l);
person.setname(
"mengdee"
);
person.setage((
short
)
18
);
// 使用该方式值为null的经测试不出来,已经配置了<value>writemapnullvalue</value>
// "{"adult":false,"age":18,"gender":0,"id":1,"level":"","name":"mengdee","salary":0,"weight":0.0}"
string jsonstring = json.tojsonstring(person);
return
jsonstring;
}
@requestmapping
(
"/json3"
)
@responsebody
public
object json3(){
person person =
new
person();
person.setid(1l);
person.setname(
"mengdee"
);
person.setage((
short
)
18
);
person.setbirthday(
new
date());
object personjson = json.tojson(person);
// json.tojson(person)默认是毫秒数"birthday":1495073314780,
// 使用serializeconfig序列号配置对日期格式化
// "{"birthday":"2017-05-18 10:19:55","weight":0.0,"adult":false,"id":1,"level":"","age":18,"name":"mengdee","gender":0,"salary":0}"
string jsonstring = json.tojsonstring(personjson, serializeconfig);
return
jsonstring;
}
@requestmapping
(
"/json4"
)
@responsebody
public
object json4(){
person person =
new
person();
person.setid(1l);
person.setname(
"mengdee"
);
person.setage((
short
)
18
);
person.setbirthday(
new
date());
person.seteducation(education.university);
// 枚举
string[] hobbies = {
"读书"
,
"旅游"
};
person.sethobbies(hobbies);
dog dog1 =
new
dog(1l,
"dog1"
, (
short
)
1
);
dog dog2 =
new
dog(2l,
"dog2"
, (
short
)
2
);
list<dog> dogs =
new
arraylist<>();
dogs.add(dog1);
dogs.add(dog2);
person.setdogs(dogs);
address address1 =
new
address(1l,
"上海浦东新区"
);
address address2 =
new
address(2l,
"上海宝山区"
);
map<string, object> addressmap =
new
hashmap<>();
addressmap.put(address1.getid() +
""
, address1);
addressmap.put(address2.getid() +
""
, address2);
person.setaddress(addressmap);
object personjson = json.tojson(person);
return
personjson;
}
@requestmapping
(
"/json5"
)
@responsebody
public
string json5(){
dog dog1 =
new
dog(1l,
"dog1"
, (
short
)
1
);
dog dog2 =
new
dog(2l,
"dog2"
, (
short
)
2
);
list<dog> dogs =
new
arraylist<>();
dogs.add(dog1);
dogs.add(dog2);
// list<t> -> json
string jsonstring = json.tojsonstring(dogs,
false
);
system.out.println(jsonstring);
// json -> list<t>
list<dog> parsearray = json.parsearray(jsonstring, dog.
class
);
for
(dog dog : parsearray) {
system.out.println(dog);
}
map<string,dog> map =
new
hashmap<string, dog>();
map.put(
"dog1"
,
new
dog(1l,
"dog1"
, (
short
)
1
));
map.put(
"dog2"
,
new
dog(2l,
"dog2"
, (
short
)
2
));
map.put(
"dog3"
,
new
dog(3l,
"dog3"
, (
short
)
3
));
// map -> json
string mapjsonstring = json.tojsonstring(map,
true
);
system.out.println(mapjsonstring);
// json -> map
@suppresswarnings
(
"unchecked"
)
map<string,dog> map1 = (map<string,dog>)json.parse(mapjsonstring);
for
(string key : map1.keyset()) {
system.out.println(key +
":"
+ map1.get(key));
}
// array -> json
string[] hobbies = {
"a"
,
"b"
,
"c"
};
string hobbiesstring = json.tojsonstring(hobbies,
true
);
system.out.println(hobbies);
// json -> array
jsonarray jsonarray = json.parsearray(hobbiesstring);
for
(object o : jsonarray) {
system.out.println(o);
}
system.out.println(jsonarray);
return
jsonstring;
}
}
|
swagger集成 。
第一步:引入相关依赖 。
1
2
3
4
5
6
7
8
9
10
11
|
<dependency>
<groupid>io.springfox</groupid>
<artifactid>springfox-swagger2</artifactid>
<version>
2.6
.
1
</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupid>com.fasterxml.jackson.core</groupid>
<artifactid>jackson-databind</artifactid>
<version>
2.6
.
6
</version>
</dependency>
|
第二步:swagger信息配置 。
swaggerconfig.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@configuration
@enablewebmvc
@enableswagger2
public
class
swaggerconfig {
@bean
public
docket customdocket() {
docket docket =
new
docket(documentationtype.swagger_2);
docket.apiinfo(apiinfo());
docket.select().apis(requesthandlerselectors.withmethodannotation(apioperation.
class
));
docket.select().paths(pathselectors.regex(
"/api/.*"
)).build();
return
docket;
}
private
apiinfo apiinfo() {
contact contact =
new
contact(
"小明"
,
"http://www.baidu.com"
,
"baidu@163.com"
);
return
new
apiinfo(
"api接口"
,
//大标题 title
"api接口"
,
//小标题
"0.0.1"
,
//版本
"www.baidu.com"
,
//termsofserviceurl
contact,
//作者
"api接口"
,
//链接显示文字
"http://www.baidu.com"
//网站链接
);
}
}
|
注意:因swaggerconfig这个类配置了注解,所以这个类必须被扫描到,即该类一定包含在context:component-scan中.
第三步:在类、方法、参数上使用注解 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
@controller
@requestmapping
(
"/api/v1"
)
@api
(description =
"api接口"
)
public
class
apicontroller {
@apioperation
(value =
"用户登录"
, notes =
"用户登录接口"
)
@apiresponses
({
@apiresponse
(code =
0
, message =
"success"
),
@apiresponse
(code =
10001
, message =
"用户名错误"
, response = illegalargumentexception.
class
),
@apiresponse
(code =
10002
, message =
"密码错误"
)
})
@requestmapping
(value =
"/user/login"
, method = requestmethod.post, produces = {
"application/json;charset=utf-8;"
})
@responsebody
public
string login(
@apiparam
(name =
"username"
, value =
"用户名"
, required =
true
)
@requestparam
string username,
@apiparam
(name =
"password"
, value =
"密码"
, required =
true
)
@requestparam
string password){
return
"{'username':'"
+ username +
"', 'password':'"
+ password +
"'}"
;
}
@apiimplicitparams
({
@apiimplicitparam
(paramtype =
"header"
, name =
"phone"
, datatype =
"string"
, required =
true
, value =
"手机号"
),
@apiimplicitparam
(paramtype =
"query"
, name =
"nickname"
, datatype =
"string"
, required =
true
, value =
"nickname"
, defaultvalue =
"双击666"
),
@apiimplicitparam
(paramtype =
"path"
, name =
"platform"
, datatype =
"string"
, required =
true
, value =
"平台"
, defaultvalue =
"pc"
),
@apiimplicitparam
(paramtype =
"body"
, name =
"password"
, datatype =
"string"
, required =
true
, value =
"密码"
)
})
@requestmapping
(value =
"/{platform}/user/regist"
, method = requestmethod.post, produces = {
"application/json;charset=utf-8;"
})
@responsebody
public
string regist(
@requestheader
string phone,
@requestparam
string nickname,
@pathvariable
string platform,
@requestbody
string password){
return
"{'username':'"
+ phone +
"', 'nickname':'"
+ nickname +
"', 'platform': '"
+ platform +
"', 'password':'"
+password+
"'}"
;
}
@requestmapping
(value =
"/user/list"
, method = requestmethod.get, produces = {
"application/json;charset=utf-8;"
})
@responsebody
public
string getuserlist(pager pager){
return
"[{'id': "
+pager.getpage()+
", 'username': 'zhangsan"
+pager.getsize()+
"'}]"
;
}
@requestmapping
(
"/docs"
)
@apiignore
public
string test(){
return
"api-docs"
;
}
}
|
pager 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public
class
pager {
@apimodelproperty
(value =
"页码"
, required =
true
)
private
int
page;
@apimodelproperty
(value =
"每页条数"
, required =
true
)
private
int
size;
public
pager() {
}
public
int
getpage() {
return
page;
}
public
void
setpage(
int
page) {
this
.page = page;
}
public
int
getsize() {
return
size;
}
public
void
setsize(
int
size) {
this
.size = size;
}
}
|
常用注解:
第四步:访问/v2/api-docs 。
在浏览器上访问http://localhost:8080/工程名称/v2/api-docs 如果有json内容,证明正常 。
第五步:下载swagger-ui 。
从github上下载https://github.com/swagger-api/swagger-ui,注意这里要选择下载v2.2.10(https://github.com/swagger-api/swagger-ui/tree/v2.2.10 ),大于这个版本的集成方式不一样.
集成方法:将v2.2.10下的dist目录下的所有文件放到自己工程中静态文件中,并使用下面代码覆盖掉index.html中的脚本部分 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
<script type=
"text/javascript"
>
var baseurl =
""
;
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if
(url && url.length >
1
) {
url = decodeuricomponent(url[
1
]);
}
else
{
//上面描述的api-docs地址
url = baseurl +
"/webapp/v2/api-docs"
;
}
// pre load translate...
if
(window.swaggertranslator) {
window.swaggertranslator.translate();
}
window.swaggerui =
new
swaggerui({
url: url,
validatorurl: undefined,
dom_id:
"swagger-ui-container"
,
supportedsubmitmethods: [
'get'
,
'post'
,
'put'
,
'delete'
,
'patch'
],
oncomplete: function (swaggerapi, swaggerui) {
if
(typeof initoauth ==
"function"
) {
initoauth({
clientid:
"your-client-id"
,
clientsecret:
"your-client-secret-if-required"
,
realm:
"your-realms"
,
appname:
"your-app-name"
,
scopeseparator:
","
,
additionalquerystringparams: {}
});
}
if
(window.swaggertranslator) {
window.swaggertranslator.translate();
}
$(
'pre code'
).each(function (i, e) {
hljs.highlightblock(e)
});
addapikeyauthorization();
},
onfailure: function (data) {
log(
"unable to load swaggerui"
);
},
docexpansion:
"none"
,
jsoneditor:
false
,
apissorter:
"alpha"
,
defaultmodelrendering:
'schema'
,
showrequestheaders:
false
});
//这里可以添加权限认证,例如token
function addapikeyauthorization() {
var token =
"you-token"
;
var tokenheader =
new
swaggerclient.apikeyauthorization(
"token"
, token,
"header"
);
window.swaggerui.api.clientauthorizations.add(
"token"
, tokenheader);
}
window.swaggerui.load();
function log() {
if
(
'console'
in window) {
console.log.apply(console, arguments);
}
}
});
</script>
|
第六步:访问上面修改的那个index.html http://localhost:8080/工程名称/static/third-party/swagger-ui/index.html 。
注意:因要访问静态资源,使用springmvc请确保静态资源能够被访问到,如果不能访问请做如下配置:
1、 在spring的配置文件中增加默认的servlet处理器 。
1
2
|
<!-- 过滤静态资源 -->
<mvc:
default
-servlet-handler/>
|
2、 在web.xml中增加要过滤的静态文件 。
1
2
3
4
5
6
7
8
|
<!-- 过滤静态资源 -->
<servlet-mapping>
<servlet-name>
default
</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>/assets/*"</url-pattern>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
|
示例项目代码结构:
完整示例demo下载地址:platform-springmvc-webapp.rar 。
其他 。
关于spring-servlet.xml 和 applicationcontext.xml 。
springmvc 提供了两种配置文件 spring-servlet.xml 、 applicationcontext.xml spring-servlet.xml 是controller级别的,作用范围是控制层,默认的名字是【servlet-name】-servlet.xml 默认是放在web-inf/目录下,springmvc会自动加载,也可以在web.xml中配置它的位置 。
1
2
3
4
5
6
7
8
9
|
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-
class
>org.springframework.web.servlet.dispatcherservlet</servlet-
class
>
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
|
一般在spring-servlet.xml中配置一些和控制器相关的配置,如视图解析、静态资源文件的映射、返回结果的解析等 。
视图解析 。
静态资源映射 。
1
2
3
4
5
6
|
<mvc:resources location=
"/static/"
mapping=
"/static/**"
/>
<mvc:resources location=
"/images/"
mapping=
"/static/**"
/>
<mvc:resources location=
"/css/"
mapping=
"/static/**"
/>
<mvc:resources location=
"/js/"
mapping=
"/static/**"
/>
<mvc:resources location=
"/html/"
mapping=
"/static/**"
/>
<mvc:resources location=
"/upload/"
mapping=
"/static/**"
/>
|
org.springframework.context.support.resourcebundlemessagesource 。
返回结果的解析 。
3、applicationcontext.xml 是系统级别的配置,作用范围是系统上下文,它的初始化需要放到 web.xml 中的context-param中配置 。
1
2
3
4
|
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:conf/spring/spring-*.xml</param-value>
</context-param>
|
4、 关于applicationcontxt.xml,一般是按照功能拆成多个配置文件如:
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.
原文链接:https://blog.csdn.net/vbirdbest/article/details/72472149 。
最后此篇关于Spring MVC+FastJson+Swagger集成的完整实例教程的文章就讲到这里了,如果你想了解更多关于Spring MVC+FastJson+Swagger集成的完整实例教程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Fastjson介绍 Fastjson是一个Java语言编写的JSON处理器。 1、遵循http://json.org标准,为其官方网站收录的参考实现之一。 2、功能qiang打,支持JDK的
1、介绍 2、Maven依赖 3、JSON 3.1、String转JSON 1、JSON对象字符串 转为 JSON对象 2、JSON数组字符串 转为 JSON数组 3、JSON对象字符串 转为 Jav
所以有点像我之前的问题的基础。我正在尝试保存蓝图,它只是游戏对象/实体的一堆设置。我现在将组件(及其设置)存储为 List (IEntityComponent 是任何组件的接口(interface))
我已经开始使用 FastJSON,但在使用过程中遇到了一些问题。我在 Internet 上找不到任何指南或文档,只能在 CodeProject 中找到一些摘录。 例如:我有这门课: [Serializ
Fastjson是一个Java语言编写的高性能功能完善的JSON库。将解析json的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协
实际项目中,我们经常需要使用序列化工具来存储和传输对象。目前用得比较多的序列化工具有:jackson、fastjson、kryo、protostuff、fst 等,本文将简单对比这几款工具序列化和反序
我想要使用 fastJSON 反序列化部分数据失败。 class A { public String a; } 数据 {"a": "a", "b": "b" } 使用 A objectA =
我就废话不多说了,大家还是直接看代码吧~ ? 1
基础部分 1. fastjson 简介 fastjson是一个java库,可用于将java对象转换为json表示。它也可以被用来将一个json字符串转换成一个等效的java对象。在转换速度上应该
第一种情况是从后台拿到数据,进行反序列化,反序列化格式时间:试了一下很多网上的方法,最后发现还是在实体类上面的日期字段加上如下注解,可以完成格式化操作,否则默认就都是时间戳的格式: @JSONFi
一:hibernate-validator 基础 1. 简介: 通过使用注解annotations 给类或者类的属性加上约束(constraint),在运行期检查属性值的合法性. 2. 作用:
===== UPDATED 8/20/2016 ===== latest version of fastjson can now handle Dictionary type correctly, m
我正在尝试在 Kotlin 代码中使用 fastjson 解析对象。但是当我使用 JSON.parseObject 时发生异常,以下是详细信息: 我的数据类: import com.aliba
我无法在现有线程中找到我的问题的答案。我有这个 JSON 字符串: { "Count": 4, "Result:000": { "Score": 4.571,
我正在使用 fastJSON我遇到了一个问题。我无法将 JSON 字符串转换为对象集合。 我认为它可以解决这个问题,但也许我做错了或被误解了。 Handles polymorphic collecti
为了将空字符串转换为“”,引入了FastJsonHttpMessageConverter。 Controller 定义为: 定义的请求模型为:enter image description here
我是一名优秀的程序员,十分优秀!