【发布时间】:2017-02-18 14:44:44
【问题描述】:
我正在用 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();
}
}
}
Resource class(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();
}
【问题讨论】:
-
你最后的战争名称是什么?
-
org.CTAG.DATEX2REST部分是什么?
标签: java rest maven jersey tomcat8