我正在开发一个REST Web服务,该服务应该能够根据URL参数响应XML。我有这个应用程序的第一个版本作为本地,因此我有一个初始化XML处理的POJO主类。问题是无法找到资源,也许我把错误的POJO main方法放在Application类中。此 URI 正在使用localhost:8090 / org.CTAG.DATEX2REST,但如果添加位置网址路径,则无效。
PD:我使用的是jersey framework,maven,JAXB和Tomcat8
EDITED:我删除了Application类,现在POJO main是一个ServletContextListener
休息班:
package com.CTAG.rest;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import javax.ws.rs.GET;
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.CTAG.core.Traslator;
import eu.datex2.schema._2_0._2_3.D2LogicalModel;
import eu.datex2.schema._2_0._2_3.Situation;
import eu.datex2.schema._2_0._2_3.SituationPublication;
import com.CTAG.filters.FilterByRoad;
@Path("/datex")
@Produces(MediaType.APPLICATION_XML)
public class DataExchange {
private D2LogicalModel datex2 = Traslator.d2;
@GET
@Produces(MediaType.APPLICATION_XML)
public Response getDatex() {
return Response.ok(this.datex2).build();
}
@GET
@Path("/{road}")
public Response getDatexByRoad(@PathParam("road") String roadName){
SituationPublication payLoad = (SituationPublication)this.datex2.getPayloadPublication();
FilterByRoad filter = new FilterByRoad(payLoad.getSituation());
List<Situation> filteredList = new LinkedList<>();
filteredList.addAll(filter.filterByRoad(roadName));
payLoad.setSituation(filteredList);
this.datex2.setPayloadPublication(payLoad);
return Response.ok(this.datex2).build();
}
}
申请类:
package com.CTAG.application;
import eu.datex2.schema._2_0._2_3.D2LogicalModel;
import eu.datex2.schema._2_0._2_3.SituationRecord;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import com.CTAG.core.Traslator;
import static java.lang.Thread.sleep;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Configuration of the REST application. This class includes the resources and
* configuration parameter used in the REST API of the application.
*
* @author Andoni Da Silva
*
*/
@ApplicationPath("/rest")
public class ExchangeApplication extends Application{
@Override
public Set<Class<?>> getClasses() {
return Stream.of(D2LogicalModel.class)
.collect(Collectors.toSet());
}
//Old POJO main method
public static void init() throws InterruptedException {
try {
Map<SituationRecord, Integer> map = new HashMap<> ();
while (true) {
URL url = new URL (" http://infocar.dgt.es/datex2/dgt/SituationPublication/all/content.xml");
Map<SituationRecord, Integer> copia = map;
map = Traslator.traslator (copia, url);
sleep (120000);
//Preubassleep(30000);
}
} catch (TransformerConfigurationException | JAXBException | ParserConfigurationException | IOException e) {
e.printStackTrace ();
}
}
}
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>CTAG DATEX2</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>org.CTAG.DATEX2REST</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</servlet-name>
<url-pattern>/rest*</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
似乎你的servlet映射是错误的,应该是类似下面的内容,你需要告诉servlet从哪里选择带注释的类
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.CTAG.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
答案 1 :(得分:0)
<!-- Auto scan REST service (false in case resteasy.resources is given ) -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</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>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.test.rest.service.HelloWorldService</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>
答案 2 :(得分:0)
我已经解决了这个问题,就是我将web.xml放在错误的目录中,并且它必须在WebContent / WEB_INF / web.xml中,因此它被忽略了,因为事实上mvn没有如果未到达web.xml,则会失败。此外,POJO主类被ServletContextListener替换,并且还删除了Application类。