一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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  : [email protected]
 * @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  : [email protected]
 * @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精品婷婷国产综合久久8 | 免费日本在线视频 | 欧美综合色网 | 性趣用品 | 天码毛片一区二区三区入口 | 免费的强动漫人物的 | 特黄特色大片免费视频播放 | 无人在线视频高清免费播放 | 国产爽视频 | 九九热视频免费观看 | 99精品视频免费 | 不卡一区二区三区 | 国产91精品久久久久久 | 金莲你下面好紧夹得我好爽 | 蛮荒的童话未删减在线观看 | 欧美视频在线播放观看免费福利资源 | 精品视频一区二区三区 | 午夜私人影院在线观看 视频 | 欧美在线成人免费国产 | 女人张开腿让男人桶爽 | 奇米影视奇米色777欧美 | 欧美特黄特色aaa大片免费看 | boobsmilking流奶水野战 | 美女脱了内裤张开腿亲吻男生 | chinese调教踩踏视频 | 日本最新伦中文字幕 | 99在线视频精品费观看视 | 日韩 国产 欧美 精品 在线 | 欧美伦理影院 | 国内精品视频九九九九 | 乌克兰黄色录像 | 国产成人精品免费大全 | 500福利第一导航 | 日本漫画工囗全彩内番e绅 日本伦理动漫在线观看 | 国产精品视频第一页 | 国产午夜永久福利视频在线观看 | 国产精品久久久久久久久久久久久久 | 男人吃奶动态图 | 啊好痛嗯轻一点免费 | 大学生情侣在线 |