Первый пример получаю 404 не находит, как токо меняю на этот код (1.снизу) + web.xml ..
Идёт обращение по:
localhost:8080/Testing/register/get при загрузки сервреа..
Использую IDEA - привожу пример настроик:
мой web.xml:
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>demo.JerseyDemo</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register/*</url-pattern>
</servlet-mapping>
1.
@Path("/register")
public class JerseyDemo {
@GET
@Path("/get")
public String getValue(@QueryParam("value") String value){
System.out.println("The Value is: "+value); // will have output null !
return value;
}
}
Второй пример работает, не создаю web.xml Идёт обращение по:
localhost:8080/Testing/register
2.
@WebServlet(urlPatterns = "/register")
public class JerseyDemo extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// code working
}
}
Tomcat localhost log:
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Изменил на: при старте сервера обращение по URL:
localhost:8080/Testing/jersey/register/get
public class ApplTest {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Response request = client.target("http://localhost:8080/Testing/jersey/register/get").request().get();
System.out.println(request);
}
}
InboundJaxrsResponse{context=ClientResponse{method=GET, uri=localhost:8080/Testing/jersey/register/get, status=404, reason=Not Found}}
@Path("/register")
public class JerseyDemo {
@Context
HttpServletResponse httpServletResponse;
@GET
@Path("/get")
public void testServlet() throws IOException {
httpServletResponse.getWriter().print("Hello");
System.out.println("Hello!");
}
}
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>demo</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/jersey/*</url-pattern>
</servlet-mapping>
</web-app>