Spring Boot Implementation RESTful WebService server example
1.Spring Boot Configuration
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 Launch Application Witapp.java
/* * Copyright 2016-2017 witpool.org All rights Reserved.
* * 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 th is file. This file was distributed * on an ' as is ' BASIS, without warranties or CONDITIONS of any KIND, either * Express OR Implie
D. 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.
* * 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 th is file. This file was distributed * on an ' as is ' BASIS, without warranties or CONDITIONS of any KIND, either * Express OR Implie
D. 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@out look.com * @Date: 2017-11-15 PM 2:50:27 * @Version: 1.0 */@RestController @RequestMapping ("/users") publi
C 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 W
Itutil.success (Reposit.save (user)); }/** * * @Title: AddUsers * @Description: ADD users by specified number * @param @param num * @param @return * @return witresult<wituser> * @throws * * * @PostMapping (value = "/{number}") pub
Lic 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> UPDATEU Ser (@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 witre Sult<wituser> Getuserbyid (@PathVariable ("userid") Integer userId) {return witutil.success (Reposit.findo
NE (userId)); }/** * * @Title: Getuserbyname * @Description: Get user by name * @param @param userName * @p Aram @return * @return Witresult<wituser> * @throws * * * @GetMapping (value = "/name/{username}") Public witresult<wituser> ge Tuserbyname (@PathVariable ("UserName") String userName) {return witutil.success (Reposit.findbyusername (Usernam
e)); }/** * * @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.
* * 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 th is file. This file was distributed * on an ' as is ' BASIS, without warranties or CONDITIONS of any KIND, either * Express OR Implie
D. 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.getl
Ogger (Witaspect.class);
@Pointcut ("Execution (public * org.witpool.rest.wituserrest.* (..))") public void log () {} @Before ("Log ()") public void Dobefore (Joinpoint jp) {Servletrequestatt
Ributes 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 ()); Class method Log.info ("Wit:rest class={}", Jp.getsignature (). Getdeclaringtypename() + "." + jp.getsignature (). GetName ());
Parameter 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) {L
Og.info ("wit:response={}", obj.tostring ());
}
}
5. Controller recommendation witexcepthandle.java