Spring Boot 实现Restful webservice服务端示例代码
程序员文章站
2024-04-01 20:44:22
1.spring boot configurations
application.yml
spring:
profiles:
active: dev...
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工程
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服务端示例代码,希望对大家有所帮助