随笔分类 -  Spring

摘要:## Spring RestTemplate简单说明现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClient,或者说如果我们使用Jersey这种框架的话,也会自带rest client。但是我们项目使... 阅读全文
posted @ 2015-06-20 00:57 纵酒挥刀斩人头 阅读(13604) 评论(1) 推荐(1)
摘要:在上一篇中写到了Spring MVC的异常处理,SpringMVC捕获到异常之后会转到相应的错误页面,但是我们REST API ,一般只返回结果和状态码,比如发生异常,只向客户端返回一个500的状态码,和一个错误消息。如果我们不做处理,客户端通过REST API访问,发生异常的话,会得到一个错误页面... 阅读全文
posted @ 2015-06-18 22:26 纵酒挥刀斩人头 阅读(1921) 评论(2) 推荐(0)
摘要:## Using HTTP Status Codes在我们自定义的异常上使用ResponseStatus注解。当我们的Controller抛出异常,并且没有被处理的时候,他将返回HTTP STATUS 为指定值的 HTTP RESPONSE,比如:```java @ResponseStatus... 阅读全文
posted @ 2015-06-18 20:19 纵酒挥刀斩人头 阅读(4478) 评论(0) 推荐(2)
摘要:通过Spring自定义event首先我们定义我们的event类package com.hyenas.spring.custom.event;import org.springframework.context.ApplicationEvent;public class CustomEvent extends ApplicationEvent{ private static final long serialVersionUID = -82737763905791865L; public CustomEvent(Object source) { super(so... 阅读全文
posted @ 2014-03-27 21:52 纵酒挥刀斩人头 阅读(587) 评论(0) 推荐(0)
摘要:Spring内置的event有1.ContextRefreshedEventThis event is published when theApplicationContextis either initialized or refreshed. This can also be raised using the refresh() method on theConfigurableApplicationContextinterface.2.ContextStartedEventThis event is published when theApplicationContextis start 阅读全文
posted @ 2014-03-27 20:22 纵酒挥刀斩人头 阅读(1310) 评论(0) 推荐(0)
摘要:利用spring,自己实现的一个观察者模式,写着玩玩,目的是为了加深理解,下次用Spring自带的玩一玩。首先我们定义一个侦听类接口package com.hyenas.common.listener;import java.util.Map;public interface Observer { public boolean isAsyn(); public void excute(Map params);}抽象侦听类package com.hyenas.common.listener;public abstract class AbstractObserver impl... 阅读全文
posted @ 2014-03-26 22:25 纵酒挥刀斩人头 阅读(911) 评论(0) 推荐(0)
摘要:Query query = new Query(); Criteria criteria = Criteria.where("timestamp").gt(from).lt(to); query.addCriteria(criteria); query.with(new Sort(Direction.ASC, "timestamp")); query.limit(limit); 阅读全文
posted @ 2014-03-25 20:05 纵酒挥刀斩人头 阅读(547) 评论(0) 推荐(0)
摘要:在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志。 首先我们可以写一个类实现MethodInterceptor接口: import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * 自定义 AOP 处理时间类 */public class... 阅读全文
posted @ 2013-08-13 23:09 纵酒挥刀斩人头 阅读(1180) 评论(0) 推荐(0)