为什么我无法访问JAX-RS API?

huangapple go评论67阅读模式
英文:

Why can't i access JAX-RS api?

问题

以下是翻译好的内容:

我从一家公司得到了一个任务,他们给我发送了一个已经设置好一切的虚拟机。任务是我必须创建一个API,从数据库中检索个人详细信息并显示出来。

问题是,当我运行这个应用程序时,服务器返回一个包含“hello world”文本的index.html。然而,当我尝试更改index.html时,在浏览器中并没有发生变化,但是当我通过Postman进行请求时,我得到了“已更新”的index.html。

我还意识到我无法访问我创建的API,以检查是否首先可以访问API。

index.html返回的路径是“http://hocalhost:8080/tutorial-applicans/”。

我的服务是PersonService.java:

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Stateless
@Path("person")
public class PersonService {

    @PersistenceContext(unitName = "de.erknrw_tutorial-applicants_pu")
    private EntityManager em;

    @GET
    @Path("hello")
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello World!!!";
    }
}

我正在尝试获取“Hello World!!!”,但我的路径是错误的,我尝试过“http://hocalhost:8080/tutorial-applicans/person/hello”。

也许值得一提的是还有一个JAXRSConfiguration.java文件:

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

@ApplicationPath(JAXRSConfiguration.RESTROOT)
public class JAXRSConfiguration extends Application {

    public static final String RESTROOT = "webresources";
}

如何访问sayHello()方法?路径是什么样的?

提前谢谢你。

英文:

I have a task from a company, that send me a virtual machine with everything set up. The task is that i have to create an API to retrieve Person details from the database and display it.

The problem is that when i run the application, the server returns an index.html with hello world text in it. However, when i try to change the index.html, it does not change in the browser, but when i do request through postman, i get the "updated" index.html.

What i also realised that i cannot access the API that i have created, to check if i can access APIs in the first place.

The path where the index.html returns is "http://hocalhost:8080/tutorial-applicans/"

My Service is PersonService.java:

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Stateless
@Path("person")
public class PersonService{

@PersistenceContext(unitName = "de.erknrw_tutorial-applicants_pu")
private EntityManager em;

@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
public String sayHello(){
return "Hello World!!!"
}


}



I am trying to get "Hello World!!!", but my path is wrong, when i tried "http://hocalhost:8080/tutorial-applicans/person/hello".

Might be worth mentioning that there is also a JAXRSConfiguration.java file:

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

@ApplicationPath(JAXRSConfiguration.RESTROOT)
public class JAXRSConfiguration extends Application{

public static final String RESTROOT = "webresources";
}


How do access the sayHello()? How does the path look like?

Thanks in advance

答案1

得分: 1

在部署到 Web 应用时,JAX-RS 应用程序被配置为一个 Servlet。因此,在资源路径之前,您必须添加应用程序路径。

端点将是:

http://[服务器]:[端口]/[上下文路径]/[应用程序路径]/[资源路径]/[操作路径]

在您的情况下:

http://hocalhost:8080/tutorial-applicans/webresources/person/hello
英文:

When deploying on a webapp, the JAX-RS application is configured as a Servlet. So, you have to add the application path prior to the path of the resource.

The endpoint would be:

http://[server]:[port]/[context path]/[application path]/[resource path]/[operation path]

In your case:

http://hocalhost:8080/tutorial-applicans/webresources/person/hello

huangapple
  • 本文由 发表于 2020年4月11日 00:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61144683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定