日期:2014-05-16 浏览次数:21145 次
package com.cuishen.jaxrs.demo; public interface JaxRSResource { }
package com.cuishen.jaxrs.demo; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; import com.cuishen.jaxrs.demo.factory.MyBeanFactory;; public class WinkApplication extends Application { public WinkApplication() { } @Override public Set<Object> getSingletons() { String beans[] = MyBeanFactory.getBeanNamesForType(JaxRSResource.class); Set<Object> result = new HashSet<Object>(); if (beans != null) { for (int i = 0; i < beans.length; i++) { String beanName = beans[i]; result.add(MyBeanFactory.getBean(beanName)); } } return result; } }
package com.cuishen.jaxrs.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.ws.rs.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codehaus.jackson.map.ObjectMapper; import com.cuishen.jaxrs.demo.bean.TestBean; @Path("/accountDemo") public class DemoResource implements JaxRSResource { private static final Log log = LogFactory.getLog(DemoResource.class); @POST @Path("/transactions/{id}") @Produces("application/json") public Map<String, String> getTransactions(@PathParam("id") String id) throws Exception { log.error("============================= successful getTransactions ================="); Map<String, String> map = new HashMap<String, String>(); map.put("id", id); return map; } @POST @Path("/transactions/") @Consumes("application/json") @Produces("application/json") public String getTransactions(HashMap<String, String> params) throws Exception { log.error("============================= successful getTransactions 2 ================="); List<TestBean> list = new ArrayList<TestBean>(); TestBean bean = new TestBean(); String id = (String) params.get("id"); bean.setId(id); bean.setName("xiao ming"); list.add(bean); bean = new TestBean(); bean.setId("222"); bean.setName("xiao zhang"); list.add(bean); ObjectMapper objectMapper = new ObjectMapper(); String listJson = objectMapper.writeValueAsString(list); return listJson; } @POST @Path("/demoData") @Consumes("application/x-www-form-urlencoded") @Produces("application/json") public String getDemoData(@FormParam("id") String id, @FormParam("name") String name) throws Exception { log.error("=========