1.spring boot configurations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
application.yml spring: profiles: active: dev mvc: favicon: enabled: false datasource: driver- class -name: com.mysql.jdbc.driver url: jdbc:mysql: //localhost:3306/wit_neptune?createdatabaseifnotexist=true&useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull&transformedbitisboolean=true username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true |
2.spring boot application
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
|
witapp.java /* * copyright 2016-2017 witpool.org all rights reserved. * * you may not use this file except in compliance with the license. * a copy of the license is located at * http://www.witpool.org/licenses * * or in the "license" file accompanying this file. this file is distributed * on an "as is" basis, without warranties or conditions of any kind, either * express or implied. see the license for the specific language governing * permissions and limitations under the license. */ package org.witpool; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; /** * @classname: witapp * @description: witpool application * @author dom wang * @date 2017-11-15 am 11:21:55 * @version 1.0 */ @springbootapplication public class witapp { public static void main(string[] args) { springapplication.run(witapp. class , args); } } |
3.rest controller
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
|
wituserrest.java /* * copyright 2016-2017 witpool.org all rights reserved. * * you may not use this file except in compliance with the license. * a copy of the license is located at * http://www.witpool.org/licenses * * or in the "license" file accompanying this file. this file is distributed * on an "as is" basis, without warranties or conditions of any kind, either * express or implied. see the license for the specific language governing * permissions and limitations under the license. */ package org.witpool.rest; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.deletemapping; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.putmapping; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; import org.witpool.common.enums.witcode; import org.witpool.common.model.bean.witresult; import org.witpool.common.model.po.wituser; import org.witpool.common.util.witutil; import org.witpool.persist.witrepository; import org.witpool.service.witservice; /** * @class name : wituserrest * @description: witpool user rest * @author : dom wang * @email : witpool@outlook.com * @date : 2017-11-15 pm 2:50:27 * @version : 1.0 */ @restcontroller @requestmapping("/users") public class wituserrest { private final static logger log = loggerfactory.getlogger(wituserrest.class); @autowired private witrepository reposit; @autowired private witservice service; /** * * @title: adduser * @description: add one user * @param @param user * @param @return * @return witresult<wituser> * @throws */ @postmapping public witresult<wituser> adduser(@requestbody wituser user) { return witutil.success(reposit.save(user)); } /** * * @title: addusers * @description: add users by specified number * @param @param num * @param @return * @return witresult<wituser> * @throws */ @postmapping(value = "/{number}") public witresult<wituser> addusers(@pathvariable("number") integer num) { if (num < 0 || num > 10) { log.error("the number should be [0, 10]"); return witutil.failure(witcode.wit_err_invalid_param); } return witutil.success(service.addusers(num)); } /** * * @title: updateuser * @description: update user * @param @param user * @param @return * @return witresult<wituser> * @throws */ @putmapping public witresult<wituser> updateuser(@requestbody wituser user) { return witutil.success(reposit.save(user)); } /** * * @title: deleteuser * @description: delete user by id * @param @param userid * @param @return * @return witresult<wituser> * @throws */ @deletemapping(value = "/{userid}") public witresult<wituser> deleteuser(@pathvariable("userid") integer userid) { reposit.delete(userid); return witutil.success(); } /** * * @title: getuserbyid * @description: get user by id * @param @param userid * @param @return * @return witresult<wituser> * @throws */ @getmapping(value = "/{userid}") public witresult<wituser> getuserbyid(@pathvariable("userid") integer userid) { return witutil.success(reposit.findone(userid)); } /** * * @title: getuserbyname * @description: get user by name * @param @param username * @param @return * @return witresult<wituser> * @throws */ @getmapping(value = "/name/{username}") public witresult<wituser> getuserbyname(@pathvariable("username") string username) { return witutil.success(reposit.findbyusername(username)); } /** * * @title: getusers * @description: get all users * @param @return * @return witresult<wituser> * @throws */ @getmapping public witresult<wituser> getusers() { return witutil.success(reposit.findall()); } } |
4.aspect
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
|
witaspect.java /* * copyright 2016-2017 witpool.org all rights reserved. * * you may not use this file except in compliance with the license. * a copy of the license is located at * http://www.witpool.org/licenses * * or in the "license" file accompanying this file. this file is distributed * on an "as is" basis, without warranties or conditions of any kind, either * express or implied. see the license for the specific language governing * permissions and limitations under the license. */ package org.witpool.common.aspect; import javax.servlet.http.httpservletrequest; import org.aspectj.lang.joinpoint; import org.aspectj.lang.annotation.after; import org.aspectj.lang.annotation.afterreturning; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.before; import org.aspectj.lang.annotation.pointcut; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.stereotype.component; import org.springframework.web.context.request.requestcontextholder; import org.springframework.web.context.request.servletrequestattributes; /** * @classname: witaspect * @description: witpool http aspect * @author dom wang * @date 2017-11-15 pm 3:36:38 * @version 1.0 */ @aspect @component public class witaspect { private final static logger log = loggerfactory.getlogger(witaspect. class ); @pointcut ( "execution(public * org.witpool.rest.wituserrest.*(..))" ) public void log() { } @before ( "log()" ) public void dobefore(joinpoint jp) { servletrequestattributes attr = (servletrequestattributes) requestcontextholder.getrequestattributes(); httpservletrequest req = attr.getrequest(); // url log.info( "wit: url={}" , req.getrequesturl()); // method log.info( "wit: http method={}" , req.getmethod()); // ip log.info( "wit: ip={}" , req.getremoteaddr()); // 类方法 log.info( "wit: rest class={}" , jp.getsignature().getdeclaringtypename() + "." + jp.getsignature().getname()); // 参数 log.info( "wit: args={}" , jp.getargs()); } @after ( "log()" ) public void doafter() { log.info( "wit: do after" ); } @afterreturning (returning = "obj" , pointcut = "log()" ) public void doafterreturning(object obj) { log.info( "wit: response={}" , obj.tostring()); } } |
5.controller advice
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
|
witexcepthandle.java /* * copyright 2016-2017 witpool.org all rights reserved. * * you may not use this file except in compliance with the license. * a copy of the license is located at * http://www.witpool.org/licenses * * or in the "license" file accompanying this file. this file is distributed * on an "as is" basis, without warranties or conditions of any kind, either * express or implied. see the license for the specific language governing * permissions and limitations under the license. */ package org.witpool.common.handle; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.bind.annotation.controlleradvice; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.responsebody; import org.witpool.common.enums.witcode; import org.witpool.common.except.witexception; import org.witpool.common.model.bean.witresult; /** * @class name: witexcepthandle * @description: witpool result * @author dom wang * @date 2017-11-15 pm 3:46:14 * @version 1.0 */ @controlleradvice public class witexcepthandle { private final static logger logger = loggerfactory.getlogger(witexcepthandle. class ); @exceptionhandler (value = exception. class ) @responsebody public witresult handle(exception e) { if (e instanceof witexception) { witexception we = (witexception) e; return new witresult(we.getcode(), we.getmessage()); } else { logger.error(witcode.wit_err_inner.getmsg() + "{}" , e); return new witresult(witcode.wit_err_inner.getcode(), e.getmessage()); } } } |
6.jpa repository
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
|
witrepository.java /* * copyright 2016-2017 witpool.org all rights reserved. * * you may not use this file except in compliance with the license. * a copy of the license is located at * http://www.witpool.org/licenses * * or in the "license" file accompanying this file. this file is distributed * on an "as is" basis, without warranties or conditions of any kind, either * express or implied. see the license for the specific language governing * permissions and limitations under the license. */ package org.witpool.persist; import java.util.list; import org.springframework.data.jpa.repository.jparepository; import org.witpool.common.model.po.wituser; /** * @class name : witrepository * @description: witpool repository * @author : dom wang * @email : witpool@outlook.com * @date : 2017-11-15 pm 2:50:27 * @version : 1.0 */ public interface witrepository extends jparepository<wituser, integer> { public list<wituser> findbyusername(string username); } |
7.代码下载、编译、打包
代码下载请访问 github上的 witpool/wit-neptune
导入工程文件、编译、打包步骤如下:
eclipse 导入maven工程
导入maven工程
maven打包
8.启动和ut步骤
启动应用:java -jar wit-rest-1.0.jar
ut步骤:
(1). 下载wisdomtool rest client
(2). 双击 jar包 restclient-1.1.jar 启动工具
导入测试用例文件:
关于wisdomtool rest client更多的使用帮助,请参考github wisdomtool/rest-client
总结
以上所述是小编给大家介绍的spring boot 实现restful webservice服务端示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://geek.csdn.net/news/detail/244296