404在Glassfish上调用Jersey 2 REST端点 [英] 404 when calling Jersey 2 REST endpoint on Glassfish
问题描述
这是我的web.xml
< servlet>
< servlet-name>模拟器HTTP API< / servlet-name>
< servlet-class> org.glassfish.jersey.servlet.ServletContainer< / servlet-class>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name>模拟器HTTP API< / servlet-name>
< url-pattern> / *< / url-pattern>
< / servlet-mapping>
< / web-app>
这是我简单的web服务:
@Path(partner)
public class PartnerAPI {
@Path(/ mt)
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sendMT(){
returnSent;
}
}
当我这样称呼它时:
http:// localhost:8080 / myprojectname / partner / mt
我发现404错误mot,我做错了什么?
更新
这是我的maven
<依赖关系>
< groupId> org.glassfish.jersey.core< / groupId>
< artifactId>球衣服务器< / artifactId>
< version> 2.22.1< / version>
< /依赖关系>
< dependency>
< groupId> org.glassfish.jersey.core< / groupId>
< artifactId> jersey-common< / artifactId>
< version> 2.22.1< / version>
< /依赖关系>
< dependency>
< groupId> org.glassfish.jersey.core< / groupId>
< artifactId>球衣客户端< / artifactId>
< version> 2.22.1< / version>
< /依赖关系>
< dependency>
< groupId> org.glassfish.jersey.containers< / groupId>
< artifactId> jersey-container-servlet< / artifactId>
< version> 2.22.1< / version>
< /依赖关系>
在Jersey 2中有不同的部署选项: p>
如果你想通过 web.xml
来完成,你必须添加一个 init-
< servlet> param
您可以指定应扫描哪些包。
< servlet-name>模拟器HTTP API< / servlet-name>
< servlet-class> org.glassfish.jersey.servlet.ServletContainer< / servlet-class>
< init-param>
< param-name> jersey.config.server.provider.packages< / param-name>
< param-value> insert.packagename.where.your.class.is.here< / param-value>
< / init-param>
< / servlet>
另一个选项是创建一个基本类来配置您的REST应用程序。
这看起来像这样:
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath(/ test)
public class YourApplication扩展ResourceConfig {
public YourApplication(){
this.packages(insert.packagename.where。 your.class.is.here);
确保更新包名称所在的字符串你的 PartnerAPI
类是。
然后在 @ApplicationPath
到您的网址。
链接如下所示: http:// localhost:8080 / myprojectname / test / partner / mt
更多信息: Jersey文档:第4章。部署RESTful Web服务
This is my web.xml
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Simulator HTTP API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
and this is my simple web service:
@Path("partner")
public class PartnerAPI {
@Path("/mt")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sendMT() {
return "Sent";
}
}
when i call it like this:
http://localhost:8080/myprojectname/partner/mt
i get 404 error mot found, what am i doing wrong?
Update
this is my maven
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
You have different deployment options in Jersey 2:
If you want to do it via web.xml
you have to add the an init-param
where you specify which packages should be scanned:
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>insert.packagename.where.your.class.is.here</param-value>
</init-param>
</servlet>
Another option is to create a basic class to configure your REST application.
This would look like this:
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/test")
public class YourApplication extends ResourceConfig {
public YourApplication() {
this.packages("insert.packagename.where.your.class.is.here");
}
}
Make sure to update the string with the package name where your PartnerAPI
class is.
Then add the value inside @ApplicationPath
to your URL.
The link would look like this: http://localhost:8080/myprojectname/test/partner/mt
More information: Jersey docs: Chapter 4. Deploying a RESTful Web Service
这篇关于404在Glassfish上调用Jersey 2 REST端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!