1.Spring Boot configurations 
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 
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 
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 
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 
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 
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打包 

8.启动和UT步骤 
启动应用:java -jar wit-rest-1.0.jar 

UT步骤: 
(1). 下载Wisdom REST Client 
(2). 双击 JAR包 restclient-1.1.jar 启动工具 
导入测试用例文件: 

Spring Boot 实现RESTful webservice服务端实例的更多相关文章

  1. Spring Boot 实现RESTful webservice服务端示例

    1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: fa ...

  2. CXF+Spring+Hibernate实现RESTful webservice服务端实例

    1.RESTful API接口定义 /* * Copyright 2016-2017 WitPool.org All Rights Reserved. * * You may not use this ...

  3. Spring Boot 集成 WebSocket 实现服务端推送消息到客户端

    假设有这样一个场景:服务端的资源经常在更新,客户端需要尽量及时地了解到这些更新发生后展示给用户,如果是 HTTP 1.1,通常会开启 ajax 请求询问服务端是否有更新,通过定时器反复轮询服务端响应的 ...

  4. Spring Boot开发RESTful接⼝服务及单元测试

    Spring Boot开发RESTful接⼝服务及单元测试 常用注解解释说明: @Controller :修饰class,⽤来创建处理http请求的对象 @RestController :Spring ...

  5. Spring Boot+CXF搭建WebService服务参考资料

    pom.xml文件引入包: <!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</gr ...

  6. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  7. Springboot监控之二:Spring Boot Admin对Springboot服务进行监控

    概述 Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增加依赖后, Spring Boot 会默认配置一些通用的监控,比如 jvm 监控.类加载.健 ...

  8. Spring Boot+CXF搭建WebService

    Spring Boot WebService开发 需要依赖Maven的Pom清单 <?xml version="1.0" encoding="UTF-8" ...

  9. DelphiXE7中创建WebService(服务端+客户端)

    相关资料: http://www.2ccc.com/news/Html/?1507.html http://www.dfwlt.com/forum.php?mod=viewthread&tid ...

随机推荐

  1. Python基础-编码与解码

      一.什么是编码 编码是指信息从一种形式或格式转换为另一种形式或格式的过程. 在计算机中,编码,简而言之,就是将人能够读懂的信息(通常称为明文)转换为计算机能够读懂的信息.众所周知,计算机能够读懂的 ...

  2. 快学Scala 1

      1. Scala解释器读到一个表达式,对它进行求值,将它打印出来,接着再继续读下一个表达式.这个过程被称作“读取-求值-打印-循环”,即REPL.   2. 从技术上来讲,scala程序并不是一个 ...

  3. 死锁与递归锁 信号量 event 线程queue

    1.死锁现象与递归锁 死锁:是指两个或两个以上的进程或线程在执行过程中,因争抢资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去,此时称系统处于死锁状态或系统产生了死锁,这些永远在互相 ...

  4. Gradle全局变量定义及引用

    在Project的build.gradle脚本中定义一些全局变量 ext { compileSdkVersion = 21 buildToolsVersion = "24.0.1" ...

  5. 大牛推荐的10本学习 Python 的好书

    Python:蛇亚目蟒科,主要包括分布于非洲及亚洲的无毒蟒蛇. Python:Richard Clabaugh拍摄的恐怖电影,2000年发行. Python:澳大利亚汽车公司. Python:英国偶发 ...

  6. .Net拾忆:HttpWebRequest/WebClient两种方式模拟Post

    一.代码 1.HttpWebRequest public static string DoPost( string target, string content ) { try { string pa ...

  7. cocos2d JS-(JavaScript) 函数类型相互转换(字符串、整形、浮点形、布尔值)

    工作忙好些天了,近段时间抽点空分享一下自己学习JS的一点笔记心得做点记录,大神勿喷,谢谢! 1.字符串的转化 var found = false; console.log(found.toString ...

  8. vue中使用base64和md5

    1.在项目根目录下安装 cnpm install --save js-base64 cnpm install --save js-md5 2.在项目文件中引入 import md5 from 'js- ...

  9. CSU 1838 Water Pump(单调栈)

    Water Pump [题目链接]Water Pump [题目类型]单调栈 &题解: 这题可以枚举缺口,共n-1个,之后把前缀面积和后缀面积用O(n)打一下表,最后总面积减去前缀的i个和后缀的 ...

  10. UVa 202 Repeating Decimals(抽屉原理)

    Repeating Decimals 紫书第3章,这哪是模拟啊,这是数论题啊 [题目链接]Repeating Decimals [题目类型]抽屉原理 &题解: n除以m的余数只能是0~m-1, ...