激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot 實現Restful webservice服務端示例代碼

Spring Boot 實現Restful webservice服務端示例代碼

2021-02-05 12:28CSDN Java教程

這篇文章主要介紹了Spring Boot 實現Restful webservice服務端示例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

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工程

Spring Boot 實現Restful webservice服務端示例代碼

導入maven工程

maven打包

Spring Boot 實現Restful webservice服務端示例代碼

Spring Boot 實現Restful webservice服務端示例代碼

8.啟動和ut步驟

啟動應用:java -jar wit-rest-1.0.jar

Spring Boot 實現Restful webservice服務端示例代碼

ut步驟:

(1). 下載wisdomtool rest client

(2). 雙擊 jar包 restclient-1.1.jar 啟動工具

導入測試用例文件:

Spring Boot 實現Restful webservice服務端示例代碼

關于wisdomtool rest client更多的使用幫助,請參考github wisdomtool/rest-client

總結

以上所述是小編給大家介紹的spring boot 實現restful webservice服務端示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://geek.csdn.net/news/detail/244296

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91高清免费 | 性欧美视频在线观看 | 西川av在线一区二区三区 | 久久久久久久高清 | 鲁丝一区二区二区四区 | 成年人在线视频观看 | 97久久曰曰久久久 | 国产91久久精品一区二区 | sesee99| 国产男女爽爽爽爽爽免费视频 | 素人视频在线观看免费 | 99国内精品视频 | 国产一级片91 | 9191久久久久视频 | 最近国产中文字幕 | 好吊色欧美一区二区三区四区 | 欧美一级做 | 国产精品久久久久久影院8一贰佰 | 亚洲成人欧美在线 | 久久久久久久久久久国产精品 | 国产精品久久久久久久午夜片 | 欧洲成人一区 | 在线影院av| 精品久久久一 | 午夜视频福利 | 日韩一级免费毛片 | 久久蜜桃精品一区二区三区综合网 | 国产激爽大片在线播放 | 视频久久免费 | 91www成人久久 | 亚洲一级电影在线观看 | 91看片网页 | 99国产精成人午夜视频一区二区 | 精品国产一区二区三区四区阿崩 | 国产日韩一区二区三区在线观看 | 国产1区在线观看 | 中文字幕在线播放不卡 | 久久久久女人精品毛片九一 | 欧美一区二区三区免费不卡 | 哪里可以看免费的av | 国产91小视频在线观看 |