2015-08-28 55 views
2

我试着编写一个简单的RESTEasy示例来了解它是如何工作的。我发现这里的信息: http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/ http://www.mkyong.com/webservices/jax-rs/resteasy-hello-world-example/简单的RESTtEasy示例不起作用

这是非常简单的,我的理解是从另一个宁静的例子是类似的,只是正常的工作原理。

@Path("/person") 
public class PersonResource { 
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(PersonResource.class); 

    private final static String FIRST_NAME = "firstName"; 
    private final static String LAST_NAME = "lastName"; 
    private final static String EMAIL = "email"; 

    private Person person = new Person(1, "Sample", "Person", "[email protected]"); 

    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String respondAsReady() { 
     return "Entered PersonResource"; 
    } 

    @GET 
    @Path("/get") 
    @Produces("application/json") 
    public Person getProductInJSON() { 
     return person; 
    } 

    @GET 
    @Path("sample") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response getSamplePerson() { 
     LOG.debug("getSamplePerson()"); 
     JSONObject jsonObject = new JSONObject(); 
     try { 
      jsonObject.put("Person First Name:", person.getFirstName()); 
      jsonObject.put("Person Last Name:", person.getLastName()); 
     } catch (JSONException e) { 
      LOG.debug("jsonObect.put failed"); 
     } 

     String result = "jsonObject:" + jsonObject; 
     return Response.status(200).entity(result).build(); 
    } 
} 

我的web.xml:

<context-param> 
    <param-name>resteasy.resources</param-name> 
    <param-value>com.restexample.PersonResource</param-value> 
</context-param> 

<listener> 
    <listener-class> 
     org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap 
    </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>/rest/*</url-pattern> 
</servlet-mapping> 

我的pom.xml:

<!-- RESTEasy--> 
<repositories> 
    <repository> 
     <id>JBoss repository</id> 
     <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> 
    </repository> 
</repositories> 


<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-jaxrs</artifactId> 
     <version>3.0.11.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>org.jboss.resteasy</groupId> 
     <artifactId>resteasy-jackson-provider</artifactId> 
     <version>3.0.11.Final</version> 
    </dependency> 
</dependencies> 

当我尝试http://localhost:8080/rest/person/sample或任何其它路径存取权限的方法有一个空白屏幕。我没有404找不到!只是空白屏幕。 (我正在使用TomCat)。谁能帮我?

+0

好吧,就像在这些例子中,我想要在浏览器中打印信息。如果我尝试[http:// localhost:8080/rest/person]我希望打印“输入PersonResource”我没有任何例外的服务器日志。 – green

+0

您的调试日志消息是否显示?你有没有使用调试器来浏览你的程序? –

+0

我的调试消息不会出现在日志文件中 – green

回答

1

我发现了什么问题。在我的pom.xml中,我有:

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

此监听器正在创建应用程序上下文。到现在为止没有错。 是在web.xml中定义的ResteasyBootstrap提供以及上下文:

<listener> 
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> 
</listener> 

因为无论听众提供不同的背景下,URL不会返回一个404 NOT FOUND,既没有好的结果。

解决办法:

删除在XML听者的ContextLoaderListener,一切都将正常工作。

0

试试这个:

@GET 
@Path("sample") 
@Produces(MediaType.APPLICATION_JSON) 
public Response getSamplePerson() { 
    LOG.debug("getSamplePerson()"); 
    return Response.status(Status.OK).entity(person).build(); 
} 
+0

它不起作用:(另一个奇怪的是我的日志文件是空的,即使我使用记录器,日志。调试不会在日志文件中产生任何输出 – green

+0

尝试并调试您的Tomcat并在'getSamplePerson()'中设置一个断点,以查看它是否在您打开URL时调用。 –