无法使用 Jersey Webservice

Unable to consume Jersey Webservice

我尝试使用 postman 使用 Jersey-2 网络服务。在我的网络服务下方

    @Path("UserService")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public class UserService {

        @POST
        @Path("/getUser")
        public Utilisateur getUser(@FormParam("identitifiant")String identifiant,@FormParam("password") String password)  {   

            UtilUtilisateur myUser = null;
            try {   
                myUser = requestUser(identifiant, password);

            } catch (Exception t) {

                log(t);
            } 
            return myUser;
        } 

在我的 web.xml 文件下面

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Rest</display-name>

    <context-param>
        <param-name>flex.class.path</param-name>
        <param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>
    </context-param>

    <!-- Http Flex Session attribute and binding listener support -->
    <listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>


    <!-- WebService Servlet -->
    <servlet>
        <servlet-name>EmagREST</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>org.glassfish.jersey.config.property.packages</param-name>
            <param-value>com.ed.eMagMoney.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>EmagREST</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

我尝试让用户在我的 URL 下面使用 postman 和 post 参数

http://localhost:8080/jersey/rest/UserService/getUser

但是 return 我有

<html>
    <head>
        <title>Apache Tomcat/6.0.16 - Rapport d'erreur</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>Etat HTTP 404 - Not Found</h1>
        <HR size="1" noshade="noshade">
            <p>
                <b>type</b> Rapport d'état
            </p>
            <p>
                <b>message</b>
                <u>Not Found</u>
            </p>
            <p>
                <b>description</b>
                <u>La ressource demandée (Not Found) n'est pas disponible.</u>
            </p>
            <HR size="1" noshade="noshade">
                <h3>Apache Tomcat/6.0.16</h3>
            </body>
        </html>

我不明白为什么,我该如何解决?

您必须使用 POST 访问您的资源,因为您使用 @POST.

注释您的方法

如果您在浏览器中输入 URL http://localhost:8080/jersey/rest/UserService/getUser 然后 GET 将被执行并获得具有给定名称和 GET 因为找不到动词。

您必须为浏览器创建客户端或获取插件才能测试 REST 服务。

  1. 请确保您的项目中包含以下所有依赖项: -jersey-container-servlet-core -泽西媒体-json-处理 -泽西媒体-json-杰克逊

  2. 为您的其余 web 服务创建一个配置 class,您还可以加载您的包以在 class 中扫描 jersey,而不是 web.xml

    import org.glassfish.jersey.jackson.JacksonFeature;
    import org.glassfish.jersey.server.ResourceConfig;
    
    public class Application extends ResourceConfig {
    
     public Application(){
     packages("com.package.of.your.resources").
     register(JacksonFeature.class);
     }
    }
    
  3. 不要忘记在你的 DAO class

  4. 中添加以下注释 @XmlRootElement
  5. 另外,在您的服务方法中,没有必要添加/(斜线)。因为路径是相对的@Path('getUser')就够了。