简体   繁体   English

tomcat 7 - 带@ApplicationPath的@GET方法 - 状态404错误

[英]tomcat 7 - @GET method with @ApplicationPath - status 404 error

i want to run my project on tomcat using endpoint path folowing are my two java files 我想在tomcat上使用端点路径运行我的项目,这是我的两个java文件

this is my app class 这是我的app类

package app;

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;

    @ApplicationPath("/t")
    public class App extends Application{

    }

this is endpoint class 这是端点类

package controllers;

import java.util.List;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import dao.IEntityDAO;
import daoimpl.EntityDAOImpl;
import dto.Contacts;
import view.ContactView;

@ApplicationPath("/t1")
public class ContactController {

    @Path("/hi")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getpassword()
    {
        return "Hiii";
    }

    @Path("/t2")
    @GET()
    @Produces(MediaType.APPLICATION_JSON)
    public List<ContactView> getallEntity(){

        IEntityDAO obj = new EntityDAOImpl();

        return obj.getallEntity();
    }       


}

my tomcat 7 is running But when i run it on tomcat by following path 我的tomcat 7正在运行但是当我通过跟踪路径在tomcat上运行它时

http://localhost:8006/ContactApp/t/t1/hi

it showing following error 它显示以下错误

HTTP Status 404 - /ContactApp/t/t1/hi


type Status report

message /ContactApp/t/t1/hi

description The requested resource is not available.


Apache Tomcat/7.0.47

anyone can help?? 谁都可以帮忙?

@ApplicationPath may only be applied to a subclass of Application : @ApplicationPath只能应用于Application的子类:

Identifies the application path that serves as the base URI for all resource URIs provided by Path. 标识作为Path提供的所有资源URI的基URI的应用程序路径。 May only be applied to a subclass of Application. 可能只适用于Application的子类。

For this one : 对于这个:

@ApplicationPath("/t")
public class App extends Application{

}

It is fine. 没事。

But it is not the case for ContactController that should not declared with @ApplicationPath : 但不应该使用@ApplicationPath声明的ContactController不是这样的:

@ApplicationPath("/t1")
public class ContactController {

but with @Path (without leading slash): 但是使用@Path (没有前导斜杠):

@Path("t1")
public class ContactController {

Extract of Path javadoc : Path javadoc的摘录:

Identifies the URI path that a resource class or class method will serve requests for. 标识资源类或类方法将为其请求的URI路径。 .... Paths are relative. ....路径是相对的。 For an annotated class the base URI is the application path, see ApplicationPath. 对于带注释的类,基URI是应用程序路径,请参阅ApplicationPath。

At last, you should remove the leading slash for the @Path of your REST methods : @Path("/t1") 最后,您应该删除REST方法的@Path的前导斜杠: @Path("/t1")

It is not required as the specification of Path explains that leading / are ignored and that base URI are handled as if a / was added. 它不是必需的,因为Path的规范说明了前导/被忽略,并且基本URI的处理就像添加了/

For the purposes of absolutizing a path against the base URI , a leading '/' in a path is ignored and base URIs are treated as if they ended in '/' 为了对基URI进行绝对路径绝对化,路径中的前导'/'被忽略,基URI被视为以'/'结尾

So these : 所以这些:

@Path("/hi")
...
@Path("/t2")

should be replaced by : 应该替换为:

@Path("hi")
...
@Path("t2")

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

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