亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot 實(shí)現(xiàn)Restful webservice服務(wù)端示例代碼

 更新時(shí)間:2017年11月16日 10:32:18   投稿:mrr  
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)Restful webservice服務(wù)端示例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

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());
  // 參數(shù)
  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.代碼下載、編譯、打包

代碼下載請(qǐng)?jiān)L問 GitHub上的 witpool/Wit-Neptune

導(dǎo)入工程文件、編譯、打包步驟如下:

Eclipse 導(dǎo)入maven工程

導(dǎo)入Maven工程

導(dǎo)入Maven工程

Maven打包

Maven install打包

生成的JAR包

8.啟動(dòng)和UT步驟

啟動(dòng)應(yīng)用:java -jar wit-rest-1.0.jar

啟動(dòng)應(yīng)用

UT步驟:

(1). 下載WisdomTool REST Client

(2). 雙擊 JAR包 restclient-1.1.jar 啟動(dòng)工具

導(dǎo)入測試用例文件:

WisdomTool RESTClient

關(guān)于WisdomTool REST Client更多的使用幫助,請(qǐng)參考GitHub wisdomtool/rest-client

總結(jié)

以上所述是小編給大家介紹的Spring Boot 實(shí)現(xiàn)Restful webservice服務(wù)端示例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • SpringMVC編程使用Controller接口實(shí)現(xiàn)控制器實(shí)例代碼

    SpringMVC編程使用Controller接口實(shí)現(xiàn)控制器實(shí)例代碼

    這篇文章主要介紹了SpringMVC編程使用Controller接口實(shí)現(xiàn)控制器實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • Java 實(shí)現(xiàn)瀏覽器下載文件及文件預(yù)覽

    Java 實(shí)現(xiàn)瀏覽器下載文件及文件預(yù)覽

    這篇文章主要介紹了Java 實(shí)現(xiàn)瀏覽器下載文件及文件預(yù)覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Spring之@Lookup注解詳細(xì)解析

    Spring之@Lookup注解詳細(xì)解析

    這篇文章主要介紹了Spring之@Lookup注解詳細(xì)解析,當(dāng)采用@Autowired注解對(duì)單例bean注依賴的原型bean時(shí),會(huì)由于單例bean只會(huì)創(chuàng)建一次,導(dǎo)致依賴的原型bean也只會(huì)注入一次,@Lookup注解可以較為優(yōu)雅的解決此類問題,需要的朋友可以參考下
    2024-01-01
  • Springboot實(shí)現(xiàn)全局自定義異常的方法詳解

    Springboot實(shí)現(xiàn)全局自定義異常的方法詳解

    這篇文章主要介紹了Springboot實(shí)現(xiàn)全局自定義異常的方法詳解,SpringBoot的項(xiàng)目已經(jīng)對(duì)有一定的異常處理了,但是對(duì)于我們開發(fā)者而言可能就不太合適了,因此我們需要對(duì)這些異常進(jìn)行統(tǒng)一的捕獲并處理,需要的朋友可以參考下
    2023-11-11
  • Java實(shí)現(xiàn)的兩種常見簡單查找算法示例【快速查找與二分查找】

    Java實(shí)現(xiàn)的兩種常見簡單查找算法示例【快速查找與二分查找】

    這篇文章主要介紹了Java實(shí)現(xiàn)的兩種常見簡單查找算法,結(jié)合具體實(shí)例形式分析了java快速查找與二分查找的原理與簡單實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-09-09
  • Java生成唯一id的幾種實(shí)現(xiàn)方式

    Java生成唯一id的幾種實(shí)現(xiàn)方式

    本文主要介紹了Java生成唯一id的幾種實(shí)現(xiàn)方式,主要介紹了5種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • Java?SWT中常見彈出框?qū)嵗偨Y(jié)

    Java?SWT中常見彈出框?qū)嵗偨Y(jié)

    剛開始寫Java工具的小伙伴可能不知道怎么寫消息對(duì)話框,在這里總結(jié)一些常用的幾種消息彈出框,下面這篇文章主要給大家介紹了關(guān)于Java?SWT中常見彈出框的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • spring security環(huán)境搭建

    spring security環(huán)境搭建

    本文通過代碼給大家介紹了spring security環(huán)境搭建的詳細(xì)教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-09-09
  • Java實(shí)現(xiàn)分頁代碼

    Java實(shí)現(xiàn)分頁代碼

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)分頁代碼,提高查詢效率,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型

    面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型

    面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型...
    2006-12-12

最新評(píng)論