繁体   English   中英

找不到RESTful球衣404

[英]RESTful jersey 404 not found

我正在使用jersey和Tomcat8开发一个ws,问题是@path没有被处理,因此生成的URL无法正常工作。

包和类描述:

eu.datex和eu.datex2包中包含带有xml注释的Java类,用于JAXB。

Transformer类将xml数据转换为java,这个java类被处理并保存在新的datex2对象中,该对象将返回到http get,以便用XML响应。

无法使用 localhost的URL :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");

}

这会奏效。

这是使用register()而不是packages()解决的,但是我无法理解为什么,谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM