简体   繁体   English

无法使用Jersey Webservice

[英]Unable to consume Jersey Webservice

I try to consume Jersey-2 webservice using postman. 我尝试使用邮递员使用Jersey-2网络服务。 below my webservice 在我的网络服务下方

    @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;
        } 

Below my web.xml file 在我的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>

I try to get user using postman below my URL with post parameters 我尝试让用户在我的URL下使用带有邮寄参数的邮递员

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

But as return I have 但是作为回报,我有

<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>

I don't understand why, How can I fix it ? 我不明白为什么,该如何解决?

You have to access your resource with POST because you annotate your method with @POST . 您必须使用POST访问资源,因为您使用@POST注释了方法。

If you enter the URL http://localhost:8080/jersey/rest/UserService/getUser in your browser then a GET will be executed and a resource with the given name and GET as verb is not found. 如果您输入的URL http://localhost:8080/jersey/rest/UserService/getUser在浏览器中,然后一个GET将被执行,以给定名称的资源并获得尽可能动词是找不到的。

You have to create either a client or get a plugin for your browser to test REST services. 您必须为浏览器创建客户端或获取插件才能测试REST服务。

  1. Be sure to have all following dependencies in your project : -jersey-container-servlet-core -jersey-media-json-processing -jersey-media-json-jackson 确保项目中具有以下所有依赖项:-jersey-container-servlet-core -jersey-media-json-processing -jersey-media-json-jackson

  2. create a configuration class for your rest webservice, you can also load your package to be scan by jersey in those class, instead in web.xml 为您的其余Web服务创建配置类,也可以在这些类中加载要由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. Don't forget to add the following annotation @XmlRootElement to your DAO class 不要忘记在您的DAO类中添加以下注释@XmlRootElement

  4. Other thing, in your service method,it's not necessary to add /(slash). 另一件事,在您的服务方法中,不需要添加/(斜杠)。 Because the path is relative @Path('getUser') is enough. 因为路径是相对的@Path('getUser')就足够了。

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

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