xxxx:8080/resteasy/messageservice/aaaa

Hello : aaaa

web.xml
<context-param>
<param-name>resteasy.guice.modules</param-name>
<param-value>com.zlg.resteasy.MyGuiceModule</param-value>
</context-param> <listener>
<listener-class>org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping> pom.xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-guice</artifactId>
<version>3.0.16.Final</version>
</dependency>
</dependencies>
<build>
<finalName>resteasy</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build> public class MyGuiceModule implements Module{ @Override
public void configure(Binder binder) {
// TODO Auto-generated method stub
binder.bind(MessageService.class);
} } @Path("/messageservice")
public class MessageService {
public MessageService(){}
@GET
@Path("/{param}")
public Response printMessage(@PathParam("param") String msg) {
String result = "Hello : " + msg; return Response.status(200).entity(result).build();
}
}

RESTEasy:JAX-RS restful webservices 示例

纯web版本

pom添加依赖:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.jboss.resteasy</groupId>
  4. <artifactId>resteasy-jaxrs</artifactId>
  5. <version>3.0.11.Final</version>
  6. </dependency>
  7. </dependencies>

web.xml添加servlet定义

  1. <servlet>
  2. <servlet-name>resteasy-servlet</servlet-name>
  3. <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  4. <init-param>
  5. <param-name>javax.ws.rs.Application</param-name>
  6. <param-value>test.MyRESTEasyApplication</param-value>
  7. </init-param>
  8. </servlet>
  9. <servlet-mapping>
  10. <servlet-name>resteasy-servlet</servlet-name>
  11. <url-pattern>/*</url-pattern>
  12. </servlet-mapping>

JAX-RS定义

  1. package test;
  2. import javax.ws.rs.core.Application;
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. public class MyRESTEasyApplication extends Application {
  6. private Set<Object> singletons = new HashSet<Object>();
  7. public MyRESTEasyApplication() { singletons.add(new HelloWorldRestService()); }
  8. @Override
  9. public Set<Object> getSingletons() { return singletons; }
  10. }
  1. package test;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.PathParam;
  5. import javax.ws.rs.core.Response;
  6. @Path("/hello")
  7. public class HelloWorldRestService {
  8. @GET
  9. public Response defaultResponse() {
  10. return Response.status(404).entity("404: Default Response ....").build();
  11. }
  12. @GET
  13. @Path("/{param}")
  14. public Response getName(@PathParam("param") String name) {
  15. String result = "RESTEasy Hello World : " + name;
  16. return Response.status(200).entity(result).build();
  17. }
  18. }

web Guice集成版本

pom添加依赖:
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.jboss.resteasy</groupId>
  4. <artifactId>resteasy-guice</artifactId>
  5. <version>3.0.16.Final</version>
  6. </dependency>
  7. </dependencies>

web.xml添加内容:

  1. <context-param>
  2. <param-name>resteasy.guice.modules</param-name>
  3. <param-value>guice.hello.MyGuiceModule</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>
  7. org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
  8. </listener-class>
  9. </listener>
  10. <servlet>
  11. <servlet-name>Resteasy</servlet-name>
  12. <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>Resteasy</servlet-name>
  16. <url-pattern>/*</url-pattern>
  17. </servlet-mapping>

java代码:

  1. package guice.hello;
  2. public interface Greeter {
  3. String greet(String name);
  4. }
  1. package guice.hello;
  2. import javax.inject.Singleton;
  3. @Singleton
  4. public class DefaultGreeter implements Greeter {
  5. public String greet(String name) {
  6. System.out.println(this);
  7. return "Hello " + name;
  8. }
  9. }
  1. package guice.hello;
  2. import com.google.inject.Binder;
  3. import com.google.inject.Module;
  4. // TODO: 可参考 core 模块中的例子:com.conquer.comutils.core.guice.MyGuiceModule
  5. public class MyGuiceModule implements Module {
  6. public void configure(final Binder binder) {
  7. binder.bind(MyResource.class);
  8. binder.bind(Greeter.class).to(DefaultGreeter.class)
  9. //                使用 javax.inject.Singleton() 标注 实现类 实现单例
  10. //                .in(com.google.inject.Scopes.SINGLETON)// 这里设置单例,默认不是单例的而是每次创建
  11. ;
  12. }
  13. }
  1. package guice.hello;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.PathParam;
  6. @Path("hello")
  7. public class MyResource {
  8. private final Greeter greeter;
  9. @Inject
  10. public MyResource(final Greeter greeter) {
  11. this.greeter = greeter;
  12. }
  13. @GET
  14. @Path("{name}")
  15. public String hello(@PathParam("name") final String name) {
  16. return greeter.greet(name);
  17. }
  18. }

@GET、@POST、@PUT、@DELETE 以及 @HEAD 均是 HTTP 请求方法指示符注释

https://www.ibm.com/developerworks/cn/web/wa-jaxrs/

@path:  顾名思义,就是请求的处理路径

Java 资源

JAX-RS 建立了一种特殊的语言来描述资源,正如由其编程模型所表示的。有五种主要条目:根资源、子资源、资源方法、子资源方法以及子资源定位器。

@Path 的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.ibm.jaxrs.sample.organization;
 
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
 
@Path(value="/contacts")
public class ContactsResource {
 
         
    @GET
    @Path(value="/{emailAddress:.+@.+\\.[a-z]+}")
    public ContactInfo getByEmailAddress(@PathParam(value="emailAddress")
        String emailAddress) {
        ...
    }
     
    @GET
    @Path(value="/{lastName}")
    public ContactInfo getByLastName(@PathParam(value="lastName") String lastName) {
        ...
    }
}

ContactsResource 类上的注释表明对 /contacts 路径的所有请求都将由 ContactsResource 根资源处理。

getByEmailAddress 上的 @Path 注释则表明任何发送到 /contacts/{emailAddress} 的请求(其中 emailAddress 代表的是正则表达式 .+@.+\\.[a-z]+)都将由 getByEmailAddress 处理。

getByLastName 方法上的 @Path 注释指定了发送到 /contacts/{lastName} 路径的所有请求(其中 lastName 代表的是一个与getByEmailAddress 内的正则表达式不匹配的有效的 URL 部分)都将由 getByLastName 方法处理。

https://www.cnblogs.com/jhcelue/p/7053959.html

http://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html_single/index.html#d4e2122

Chapter 48. Guice 3.0 Integration

RESTEasy has some simple integration with Guice 3.0.

RESTEasy will scan the binding types for a Guice Module for @Path and @Provider annotations. It will register these bindings with RESTEasy. The guice-hello project that comes in the RESTEasy examples/ directory gives a nice example of this.

@Path("hello")
public class HelloResource
{
@GET
@Path("{name}")
public String hello(@PathParam("name") final String name) {
return "Hello " + name;
}
}

First you start off by specifying a JAX-RS resource class. The HelloResource is just that. Next you create a Guice Module class that defines all your bindings:

import com.google.inject.Module;
import com.google.inject.Binder; public class HelloModule implements Module
{
public void configure(final Binder binder)
{
binder.bind(HelloResource.class);
}
}

You put all these classes somewhere within your WAR WEB-INF/classes or in a JAR within WEB-INF/lib. Then you need to create your web.xml file. You need to use the GuiceResteasyBootstrapServletContextListener as follows

<web-app>
<display-name>Guice Hello</display-name> <context-param>
<param-name>resteasy.guice.modules</param-name>
<param-value>org.jboss.resteasy.examples.guice.hello.HelloModule</param-value>
</context-param> <listener>
<listener-class>
org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
</listener-class>
</listener> <servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping> </web-app>

GuiceResteasyBootstrapServletContextListener is a subclass of ResteasyBootstrap, so you can use any other RESTEasy configuration option within your web.xml file. Also notice that there is a resteasy.guice.modules context-param. This can take a comma delimited list of class names that are Guice Modules.

48.1. Request Scope

Add the RequestScopeModule to your modules to allow objects to be scoped to the HTTP request by adding the @RequestScoped annotation to your class. All the objects injectable via the @Context annotation are also injectable, except ServletConfig and ServletContext.

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context; import org.jboss.resteasy.plugins.guice.RequestScoped; @RequestScoped
public class MyClass
{
@Inject @Context
private HttpRequest request;
}

48.2. Binding JAX-RS utilities

Add the JaxrsModule to bind javax.ws.rs.ext.RuntimeDelegate, javax.ws.rs.core.Response.ResponseBuilder, javax.ws.rs.core.UriBuilder, javax.ws.rs.core.Variant.VariantListBuilder and org.jboss.resteasy.client.ClientExecutor.

48.3. Configuring Stage

You can configure the stage Guice uses to deploy your modules by specific a context param, resteasy.guice.stage. If this value is not specified, Resteasy uses whatever Guice's default is.

<web-app>
<display-name>Guice Hello</display-name> <context-param>
<param-name>resteasy.guice.modules</param-name>
<param-value>org.jboss.resteasy.examples.guice.hello.HelloModule</param-value>
</context-param> <context-param>
<param-name>resteasy.guice.stage</param-name>
<param-value>PRODUCTION</param-value>
</context-param> <listener>
<listener-class>
org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
</listener-class>
</listener> <servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping> </web-app>

48.4. Custom Injector creation

GuiceResteasyBootstrapServletContextListener can be extended to allow more flexibility in the way the Injector and Modules are created. Three methods can be overridden: getModules(), withInjector() and getStage(). Register your subclass as the listener in the web.xml.

Override getModules() when you need to pass arguments to your modules' constructor or perform more complex operations.

Override withInjector(Injector) when you need to interact with the Injector after it has been created.

Override getStage(ServletContext) to set the Stage yourself.

<web-app>
<!-- other tags omitted -->
<listener>
<listener-class>
org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
</listener-class>
</listener>
</web-app> public class MyServletContextListener extends GuiceResteasyBootstrapServletContextListener
{ @Override
protected List<? extends Module> getModules(ServletContext context)
{
return Arrays.asList(new JpaPersistModule("consulting_hours"), new MyModule());
} @Override
public void withInjector(Injector injector)
{
injector.getInstance(PersistService.class).start();
}
}

resteasy web Guice集成版本的更多相关文章

  1. 即时通信系统中如何实现:全局系统通知,并与Web后台集成?【低调赠送:QQ高仿版GGTalk 5.1 最新源码】

    像QQ这样的即时通信软件,时不时就会从桌面的右下角弹出一个小窗口,或是显示一个广告.或是一个新闻.或是一个公告等.在这里,我们将其统称为“全局系统通知”.很多使用GGTalk的朋友都建议我加上一个类似 ...

  2. Java Web应用集成OSGI

    对OSGI的简单理解 就像Java Web应用程序需要运行在Tomcat.Weblogic这样的容器中一样.程序员开发的OSGI程序包也需要运行在OSGI容器中.目前主流的OSGI容器包括:Apach ...

  3. 即时通信系统中实现全局系统通知,并与Web后台集成【附C#开源即时通讯系统(支持广域网)——QQ高仿版IM最新源码】

    像QQ这样的即时通信软件,时不时就会从桌面的右下角弹出一个小窗口,或是显示一个广告.或是一个新闻.或是一个公告等.在这里,我们将其统称为“全局系统通知”.很多使用C#开源即时通讯系统——GGTalk的 ...

  4. SpringBoot | 第三十三章:Spring web Servcies集成和使用

    前言 最近有个单位内网系统需要对接统一门户,进行单点登录和待办事项对接功能.一般上政府系统都会要求做统一登录功能,这个没啥问题,反正业务系统都是做单点登录的,改下shiro相关类就好了.看了接入方案, ...

  5. Spring与其他Web框架集成

    Spring与多种流行Web应用框架(Struts.JSF和DWR)集成的方法. Spring强大的IoC容器和企业支持特性使其十分适于实现Java EE应用的服务和持续层. 对于表现层,可以在许多不 ...

  6. Maven实现Web应用集成測试自己主动化 -- 部署自己主动化(WebTest Maven Plugin)

    上篇:Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin) 之前介绍了怎样在maven中使用webtest插件实现web的集成測试,这里有个遗留 ...

  7. SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示

    SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示   1,与MVC集成 见http://simpleinjector.codeplex.com/wikipage? ...

  8. 跟我学Shiro---无状态 Web 应用集成

    无状态 Web 应用集成 在一些环境中,可能需要把 Web 应用做成无状态的,即服务器端无状态,就是说服务器端不会存储像会话这种东西,而是每次请求时带上相应的用户名进行登录.如一些 REST 风格的 ...

  9. Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)

    近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下.感觉很不错.对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正 ...

随机推荐

  1. BZOJ 1342: [Baltic2007]Sound静音问题 | 单调队列维护的好题

    题目: 给n个数字,一段合法区间[l,l+m-1]要求max-min<=c 输出所有合法区间的左端点,如果没有输出NONE 题解: 单调队列同时维护最大值和最小值 #include<cst ...

  2. [Leetcode] jump game ii 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. MySQL基础原创笔记(二)

    表索引关键字:PRI primary key 表示主键,唯一 写法: id bigint(20) unsigned primary key not null ,uni UNIQUE 表示唯一 id b ...

  4. Django CRM系统

    本节内容 业务痛点分析 项目需求讨论 使用场景分析 表结构设计 业务痛点分析 我2013年刚加入老男孩教育的时候,学校就一间教室,2个招生老师,招了学生后,招生老师就在自己的excel表里记录一下,每 ...

  5. Consul 入门(二)

    KV 存储 通过命令行操作 $ consul kv put hello world # 设置数据 Success! Data written to: hello $ consul kv get hel ...

  6. 淘淘相关DTO

    result 用于Controller层返回值或Controller于service层之间返回值 package com.taotao.common.pojo; import java.util.Li ...

  7. 洛谷P3966 [TJOI2013]单词(fail树性质)

    P3966 [TJOI2013]单词 题目链接:https://www.luogu.org/problemnew/show/P3966 题目描述 小张最近在忙毕设,所以一直在读论文.一篇论文是由许多单 ...

  8. ASP.NET基础学习未整理随笔

    利用新建网站添加的项目可以改完代码直接刷新就行 提交到服务程序的表单一定要具有name.<input type="hidden"value="true"n ...

  9. 仿微信中加载网页时带线行进度条的WebView的实现

    finddreams:http://blog.csdn.net/finddreams/article/details/44172639 为了仿微信中加载网页时带进度条的WebView的实现,首先我们来 ...

  10. [洛谷P2491] [SDOI2011]消防

    洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...