java - 无法使用 Jersey Web 服务

标签 java web-services jax-rs jax-ws jersey-2.0

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

    @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 下方使用带有 post 参数的 postman

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

但作为返回我有

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

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

最佳答案

  1. 确保您的项目中具有以下所有依赖项: -jersey-container-servlet-core -jersey-media-json-处理 -jersey-media-json-jackson

  2. 为您的其余 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. 不要忘记将以下注释 @XmlRootElement 添加到您的 DAO 类

  4. 另外一点,在你的服务方法中,没有必要添加/(斜杠)。因为路径是相对的 @Path('getUser') 就足够了。

关于java - 无法使用 Jersey Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29914411/

相关文章:

java - 为什么此重定向不起作用?日本科学基金会

Java 类路径,未找到类

c# - 处理来自 WS 的异常

java - 使用 Jersey、jaxb 和 jax-rs 时如何设置 xml 命名空间

java - 如何在 JAX-RS 中设置响应 header ,以便在自定义生成的文件中正确显示中文字符

java - 使用带有 Select IN 的 Spring JdbcTemplate 返回 List<String>

java - 通过调用 Activity 在服务中显示 AlertDialog

c# - .Net WebServices 和 out/ref WebMethod 参数

web-services - REST API 的目录结构

java - 对 RESTful 资源的 GET 请求导致未找到 MessageBodyWriter