pinkshining的gravatar头像
pinkshining 2016-08-18 15:18:13

spring mvc+Resteasy+Mybatis+Ehcache整合开发java webservice接口示例demo

最近要做关于移动app的web服务接口调用项目,之前没做过类似的项目,故找资料整合了springmvc+Resteasy+Mybatis+Ehcache做了一个小demo,为之后的开发奠基。

开发环境eclipse+svn+maven+mysql

项目结构图:

spring mvc+Resteasy+Mybatis+Ehcache整合开发java webservice接口示例demo

其中webservice接口代码:

@Controller
@Path("/companyws")
public class CompanyWS {
	@Autowired
	CompanyService companyService;
	
	 @GET
	 @Produces(MediaType.TEXT_HTML)
	 @Path("/index")
	   public ModelAndView viewAll()
	   {
	      return new ModelAndView("index");
	   }
	
	
	@GET
	@Path("/ehcache")
	public String getCurrentTimeMillis(@QueryParam("param")String param){
		return companyService.getCurrentTimeMillis(param);
	}
	
	@POST
	@Path("/login")
	public String login(@FormParam("username")String username,@FormParam("password")String password){
		System.out.println("-----webservice company login-----");
		String seed = companyService.login(username, password);
		String token = companyService.getCacheToken(seed);
		System.out.println(seed);
		System.out.println(token);
		return seed;
	}
	
	@POST
	@Path("/getCacheToken")
	public String getCacheToken(@FormParam("seed")String seed){
		System.out.println("-----webservice company login-----");
		return companyService.getCacheToken(seed);
	}
	
	@GET
	 @Path("/count")
	public String count(@Context HttpServletRequest request){
		System.out.println("-----webservice company count-----");
		System.out.println(companyService.total());
		return "company";
	}
	
	
	/**
	 * resteasy返回响应文件,在produces里可以写("*//*")可以表示任意文件,使用@context注解获取response"
	**/
	@Path("/getAttach/{ID}")
	@GET
	@Produces("*/*")
	public ServerResponse getAttach(@PathParam("ID") String ID,@Context HttpServletResponse response){
		Long requestTime = System.currentTimeMillis();
		OutputStream outputStream = null;
		ServerResponse sr = null;
		byte[] data = null;
		try {
			outputStream = response.getOutputStream();
			data  = "dhcdslkcd".getBytes();
			outputStream.write(data);
			outputStream.flush();
			outputStream.close();
		} catch (EOFException e) {
			e.printStackTrace();
			//sr = new ServerResponse(e.getMessage(), e.getExceptionCode());
		}catch (Exception e) {
			e.printStackTrace();
			//sr = new ServerResponse(e.getMessage(), ExceptionCode.DBPROXY_OTHER);
		}
		return sr;
	}
	
}

 

部署项目到tomcat服务器上,在浏览器输入http://localhost:8080/springmvcResteasyMybatisEhcache/companyws/count,结果如图所示:

spring mvc+Resteasy+Mybatis+Ehcache整合开发java webservice接口示例demospring mvc+Resteasy+Mybatis+Ehcache整合开发java webservice接口示例demo

具体的代码如下:


打赏

文件名:springmvcResteasyMybatisEhcache.zip,文件大小:117.803K 下载
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友