RestFull API - Jax-rs -- problem z dotarciem do endpointa

0

Cześć,

próbuje napisać web api, które będzie dostępne jako restowy endpoint.
Jednak mam problem, żeby się do niego dostać, cały czas dostaje 404.
Serwer jest postawiony lokalnie na glassfish.

próbuje dostać się do tego enpointa pod adresem http://localhost:8080/rest/some jednak bez skutku.
Podpowiecie, gdzie szukać błędu ?

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>rest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>rest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>


</project>

Servlet

package controllers;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/some")
public class HelloServlet {

    @GET
    public Response getMeyhod(){
        return Response.ok("jest ok").build();
    }
}
0

Potrzebny jest Ci ten glassfish do czegoś?
Możesz dużo prościej, jeśli po prostu chcesz Restowe API:
lub http://sparkjava.com/
lub https://ratpack.io/ http://www.baeldung.com/ratpack
lub w ostatecznośći (lubisz mieć skomplikowanie) - idź na start.spring.io i lecisz ze SpringBootem i WebFlux https://docs.spring.io/spring/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html

0

@jarekr000000: dzięki za podpowiedz ale niestety nie ja wybieram technologie. Glassfish i "czysta" javaEE wymagana przez prowadzącego na wyższej.

0

Logi z glassfish ( w sumie serwer sprawdzałem, że działa bo jak napisałem servlet, który po prostu odpowiada na tym url to działał )
Jak na moje to gdzieś w konfiguracji ścieżki do endpointa leży problem ale nie mogę go namierzyć.

start-domain domain1
Detected server admin port: 4848
Detected server http port: 8080
Active code page: 65001
Attempting to start domain1.... Please look at the server log for more details.....
Connected to server
[2018-05-01 02:24:59,624] Artifact restapi:war exploded: Artifact is being deployed, please wait...
[2018-05-01 02:25:02,805] Artifact restapi:war exploded: Artifact is deployed successfully
[2018-05-01 02:25:02,805] Artifact restapi:war exploded: Deploy took 3 181 milliseconds
[2018-05-01T14:24:59.262+0200] [glassfish 4.1] [WARNING] [] [org.glassfish.grizzly.config.Utils] [tid: _ThreadID=19 _ThreadName=RunLevelControllerThread-1525177498941] [timeMillis: 1525177499262] [levelValue: 900] [[
  Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=http-listener-1, realClassName=org.glassfish.grizzly.http2.Http2AddOn]]

[2018-05-01T14:24:59.297+0200] [glassfish 4.1] [WARNING] [] [org.glassfish.grizzly.config.Utils] [tid: _ThreadID=19 _ThreadName=RunLevelControllerThread-1525177498941] [timeMillis: 1525177499297] [levelValue: 900] [[
  Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=http-listener-2, realClassName=org.glassfish.grizzly.http2.Http2AddOn]]

[2018-05-01T14:24:59.301+0200] [glassfish 4.1] [WARNING] [] [org.glassfish.grizzly.config.Utils] [tid: _ThreadID=19 _ThreadName=RunLevelControllerThread-1525177498941] [timeMillis: 1525177499301] [levelValue: 900] [[
  Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=admin-listener, realClassName=org.glassfish.grizzly.http2.Http2AddOn]]

[2018-05-01T14:25:02.110+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Interceptor] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502110] [levelValue: 900] [[
  WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled]]

[2018-05-01T14:25:02.110+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Interceptor] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502110] [levelValue: 900] [[
  WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled]]

[2018-05-01T14:25:02.173+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502173] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] com.sun.jersey.server.impl.cdi.CDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.178+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502178] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.188+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502188] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.190+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502190] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.192+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502192] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] com.sun.jersey.server.impl.cdi.CDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.192+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502192] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.193+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502193] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.193+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502193] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.195+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502195] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.196+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502196] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] com.sun.jersey.server.impl.cdi.CDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.197+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502197] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2018-05-01T14:25:02.198+0200] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=45 _ThreadName=admin-listener(2)] [timeMillis: 1525177502198] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] com.sun.jersey.server.impl.cdi.CDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

0

A web.xml masz? Coś w tym stylu - (wziąlem z http://www.vogella.com/tutorials/REST/article.html i pozmieniałem pakiet i mapping)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>controller</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>
0
jarekr000000 napisał(a):

A web.xml masz? Coś w tym stylu - (wziąlem z http://www.vogella.com/tutorials/REST/article.html i pozmieniałem pakiet i mapping)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>controller</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

tak u mnie wygląda web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

z tym, że miałem to wykomentowane przez chwile a teraz widzę, że na tym leci błąd w logach

at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
	at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
	at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
	at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
	at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
	at javax.servlet.GenericServlet.init(GenericServlet.java:244)
	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1382)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5732)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:5977)
	at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
	at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2286)
	at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1932)
	at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
	at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
	at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
	at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
	at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
	at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
	at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
	at javax.servlet.GenericServlet.init(GenericServlet.java:244)
	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1382)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5732)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:5977)
	at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
	at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2286)
	at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1932)
	at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
	at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
  Startup of context /rest failed due to previous errors]]
0

A to :

    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2286)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1932)
    at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
    at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
  Startup of context /rest failed due to previous errors]]

wskazuje, że są jakies previous errors, więc poczytaj/wklej je.
Btw. mapping:

   <url-pattern>/rest/*</url-pattern>

To raczej jest problem. Twój context mapuje się pod /rest/ i to co jest w web.xml dotyczy wszystkiego "poniżej tej ścieżki".
dlatego raczej chcesz:

   <url-pattern>/*</url-pattern>
0

No to teraz wiadomo.
To nie powinno działać z kilku powodów.
po pierwsze w skonstruowanym warze nie ma web.xml (ale to pikuś - w sumie w nowym Java EE chyba już nawet nie wymagane - nie pamiętam).
Gorzej z tym co masz w WEB-INF/lib, a są tam jary, które na pewno się tam nie powinny znaleźć, bo robią konflikty z glassfishowymi.
Rozwiązanie - wszelkim swoim dependencyjom dodaj scope == PROVIDED:

 <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
           <scope>provided</scope>
        </dependency>

i doczytaj o maven war pluginie:
Prawdopodobnie tu musisz poprawić ścieżkę, bo twoje pliki są w src/main/web, a nie w src/main/webapp
https://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#warSourceDirectory

Najbardziej nie rozumiem ** dlaczego glassfish nie rzygał na Ciebie tonami błędów?**

0

Po 3 dniach walki znalazłem przyczynę - opisuje dla potomnych.

Okazuje się, że Jersey w wersji 2.x nie potrzebuje pliku web.xml tylko jedną klasę konfiguracyjną
Plik web.xml jest wykorzystywany tylko dla Jersay 1.x

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class ApplicationConfig extends Application {

}

Po usunięciu web.xml i dodaniu w/w klasy konfiguracyjnej aplikacja jest dostępna pod adresem -> http://localhost:8080/NazwaProjektu(artefaktu)/rest

1 użytkowników online, w tym zalogowanych: 0, gości: 1