2016-05-15 32 views
-1

我爲基於Java EE的項目創建了一個簡單的JAX-RS資源類。我試圖從服務器得到迴應,但當我訪問我的網址時,我得到一個錯誤。這是我寫的類:如何訪問JAX-RS資源?

@Path("/rest") 
public class RestResource { 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public String create(String name) 
    { 
     return "print my string"; 
    } 
} 

我搞不​​清楚我做錯了什麼。 我必須在我的web.xml中定義一個新的servlet嗎?我現在的有什麼問題嗎?

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    metadata-complete="false"> 

    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 

    <display-name>crud-app</display-name> 

    <!-- Activate the JSF 2.0 servlet --> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- Tell the context which URLs to send through JSF --> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

    <!-- This is an optional parameter, but it makes troubleshooting errors 
     much easier --> 
    <!-- You are advised to remove this context parameter before a production 
     deployment --> 
    <context-param> 
     <param-name>facelets.DEVELOPMENT</param-name> 
     <param-value>true</param-value> 
    </context-param> 

    <!-- Welcome page --> 
    <welcome-file-list> 
     <welcome-file>person.jsf</welcome-file> 
    </welcome-file-list> 

    <error-page> 
     <exception-type>java.lang.Throwable</exception-type> 
     <location>/ui/error/error.jsf</location> 
    </error-page> 
    <error-page> 
     <exception-type>javax.faces.application.ViewExpiredException</exception-type> 
     <location>/ui/error/viewExpired.jsf</location> 
    </error-page> 
</web-app> 
+0

你將這部署到什麼?它是一個企業容器還是像tomcat一樣的servlet容器? –

+0

我將它部署到野蠻服務器。我設法通過使用resteasy解決我的問題 – ffs

回答

0

我設法通過使用RESTEasy解決了我的問題。這裏是我更新的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    metadata-complete="false"> 

    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 

    <display-name>crud-app</display-name> 

    <!-- Activate the JSF 2.0 servlet --> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- Tell the context which URLs to send through JSF --> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

    <!-- Auto scan REST service --> 
    <context-param> 
     <param-name>resteasy.scan</param-name> 
     <param-value>true</param-value> 
    </context-param> 

    <!-- this need same with resteasy servlet url-pattern --> 
    <context-param> 
     <param-name>resteasy.servlet.mapping.prefix</param-name> 
     <param-value>/rest</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> 

    <!-- This is an optional parameter, but it makes troubleshooting errors 
     much easier --> 
    <!-- You are advised to remove this context parameter before a production 
     deployment --> 
    <context-param> 
     <param-name>facelets.DEVELOPMENT</param-name> 
     <param-value>true</param-value> 
    </context-param> 

    <!-- Welcome page --> 
    <welcome-file-list> 
     <welcome-file>person.jsf</welcome-file> 
    </welcome-file-list> 

    <error-page> 
     <exception-type>java.lang.Throwable</exception-type> 
     <location>/ui/error/error.jsf</location> 
    </error-page> 
    <error-page> 
     <exception-type>javax.faces.application.ViewExpiredException</exception-type> 
     <location>/ui/error/viewExpired.jsf</location> 
    </error-page> 
</web-app>