我有一个使用IntelliJ 2016
制作并部署在Tomcat 8
上的网络应用。奇怪的是,当IntelliJ触发浏览器并加载页面时,页面加载良好,但是如果我按F5并重新加载页面,我会从Tomcat中找到404 Not ...我正在使用Maven
作为依赖项和Jason
的{{1}} ...我在Rest API
标记的web.xml
文件中也注意到了一件罕见的事情。该标签有“/ *”作为值,在服务中(在客户端)我用“rest / mascotas”url调用REST API,没有“/ mascotas”只有没有“rest”部分...所以我不喜欢不知道为什么会这样......我给你留下了servlet定义,web.xml以及调用servlet的客户端中的服务。
PD:我正在为客户使用AngularJS。
的web.xml:
<url-pattern>
MascotaService.java:
<?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">
<display-name>MissingDog</display-name>
<servlet>
<servlet-name>Mascotas-Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.missingdog.services</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Mascotas-Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<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>
</web-app>
MascotaService.js:
package com.missingdog.services;
import java.io.IOException;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.missingdog.dao.MascotaDAO;
import com.missingdog.model.Mascota;
@Path("mascotas")
public class MascotaService {
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response listar() throws JsonProcessingException {
MascotaDAO dao = new MascotaDAO();
List<Mascota> list = null;
try {
list = dao.listarMascotas();
System.out.println(list.size());
} catch (Exception e) {
return Response.status(500).build();
}
ObjectMapper objectMapper = new ObjectMapper();
//Habilita el indentando hacia la derecha del codigo JSON generado
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
String output = objectMapper.writeValueAsString(list);
return Response.status(200).entity(output).build();
}
@POST
@Path("/{mascota}")
@Consumes({MediaType.APPLICATION_JSON})
public Response guardar(@PathParam("mascota") String jsonMascota) {
//Convierto los datos enviados por el cliente en formato JSON en un objeto tipo Mascota
ObjectMapper objectMapper = new ObjectMapper();
Mascota mascota = null;
try {
mascota = objectMapper.readValue(jsonMascota, Mascota.class);
} catch (IOException e1) {
return Response.status(500).build();
}
MascotaDAO dao = new MascotaDAO();
try {
dao.guardar(mascota);
} catch (Exception e) {
return Response.status(500).build();
}
return Response.status(204).build();
}
@DELETE
@Path("/{id}")
@Consumes({MediaType.APPLICATION_JSON})
public Response eliminar(@PathParam("id") Integer id) {
MascotaDAO dao = new MascotaDAO();
try {
dao.eliminar(id);
} catch (Exception e) {
return Response.status(500).build();
}
return Response.status(204).build();
}
}