2016-05-15 7 views
-1

Java EEベースのプロジェクト用のシンプルなJAX-RSリソースクラスを作成しました。私はサーバーから応答を返そうとしていますが、私のURLにアクセスするとエラーが返ってきます。これは私が書いたクラスです:JAX-RSリソースにはどのようにアクセスできますか?

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

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

私が間違っていることを理解できません。 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> 

    <!-- 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のようなサーブレットコンテナですか? –

+0

私はそれをwildflyサーバーに展開しています。私は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> 
関連する問題