我正在使用 jersey 和 Tomcat8 开发一个 ws,问题是 @path 没有被处理,因此生成的 URL 无法正常工作。
包和类描述:
eu.datex 和 eu.datex2 包包含带有用于 JAXB 的 xml 注释的 java 类。
Transformer 类将 xml 数据转换为 java,该 java 类被处理并保存在新的 datex2 对象中,该对象将作为 http get 返回,以便使用 XML 进行响应。
URL 不起作用 localhost:8090/org.CTAG.DATEX2REST/rest/datex
这里我向大家展示我的mvn项目结构和一些重要文件。
mvn结构:
这是 ResourceConfig 类:
package com.CTAG.application;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/rest")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("com.CTAG.rest;");
}
}
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" version="3.1">
<display-name>org.CTAG.DATEX2REST</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>
<display-name>CTAG DATEX2</display-name>
<listener>
<listener-class>
com.CTAG.application.Init
</listener-class>
</listener>
</web-app>
此类使用 JAXB 初始化从 xml 数据(从服务器 GET)到 Java 类的转换:
public class Init implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("----INITIALIZED----");
try {
Map<SituationRecord, Integer> map = new HashMap<>();
URL url = new URL(" http://infocar.dgt.es/datex2/dgt/SituationPublication/all/content.xml");
Map<SituationRecord, Integer> copia = map;
map = Traslator.traslator(copia, url);
System.out.println("----DATEX now available----");
// Preubassleep(30000);
} catch (TransformerConfigurationException | JAXBException | ParserConfigurationException | IOException e) {
e.printStackTrace();
}
}
}
资源类(DataExchange),这必须返回一个将转换为 XML 的 java 类:
package com.CTAG.rest;
@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();
}
最佳答案
替换
{
packages("com.CTAG.rest;");
}
到
{
packages("com.CTAG.rest");
}
这会起作用。
关于java - RESTful Jersey 404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39953858/