我在动态Web项目中与Jersey一起制作了Web服务。现在,我想在不使用Maven的情况下构建它(我们的教授不会让我们使用Maven)。我在Tomcat 9.0服务器上运行该项目。它运行完美,但是当我访问localhost:8080/agendarest/contactos
时,我会收到404错误。
服务:
@Path("/contactos")
public class ContactoService {
private static final long serialVersionUID = 1L;
@GET
@Produces("application/json")
public Response getUsers() throws JSONException{
List<Contacto> contactos = new ArrayList<>();
contactos.add(new Contacto("pedro", 3434661825L));
return Response.status(200).entity(contactos.toString()).build();
}
}
web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>agendarest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我找到了解决方案。我需要添加init-param所以,我的web.xml:
<servlet>
<servlet-name>ContactoService</servlet-name>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.MiApp</param-value>
</init-param>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ContactoService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
并添加miapp.java:
import org.glassfish.jersey.server.ResourceConfig;
public class MiApp extends ResourceConfig{
public MiApp() {
packages("com");
}
}