1

好的,我有一个包含两个 RESTful Web 服务类 Login.class 和 Subscribe.class 的包。当我将它们部署到应用程序引擎并对其进行测试时,它们可以正常工作。现在,我将一个新的服务类名称 User s.class 添加到包中,现在当我调用它们时,我的服务都不起作用,我收到内部服务器错误。

com.sun.jersey.spi.inject.Errors processErrorMessages: The following errors and warnings have been detected with resource and/or provider classes:

严重:产生媒体类型冲突。资源方法 public javax.ws.rs.core.Response com.ppsjamaica.webservices.PPSUsers.getUserById(java.lang.Long) 和 public javax.ws.rs.core.Response com.ppsjamaica.webservices.PPSUsers.getUserByUsernamePassword( java.lang.String,java.lang.String) 可以产生相同的媒体类型

Uncaught exception from servlet

com.sun.jersey.spi.inject.Errors.processErrorMessages (Errors.java:170) 处的 com.sun.jersey.spi.inject.Errors$ErrorMessagesException

我的订阅课程是:

   @Path ("/subscriber")
public class Subscribe {

    @GET
    @Produces (MediaType.APPLICATION_JSON)
    public Response getSubscriber (@QueryParam("email")String email)
    {
        EntityManager entityManager = EMF.getInstance ().createEntityManager ();

        /*
         * Sends back a BAD_REQUEST HTTP status
         * if email query parameter is empty
         */
        if (email.isEmpty ())
            return Response.status(Status.BAD_REQUEST).build ();

        try
        {
            Query query = entityManager.createQuery ("SELECT FROM Subscriber WHERE email= :emailParam");
            query.setParameter("emailParam", email);
            return Response.status(Status.OK).entity((Subscriber) query.getSingleResult ()).build (); 
        }
        catch (NoResultException e)
        {
            return Response.status (Status.NOT_FOUND).build ();
        }

    }//end Subscriber method
}

我的登录类是:

    @Path ("/login")
public class Login {

    @GET
    @Produces (MediaType.APPLICATION_JSON)
    public User authenticate (@QueryParam("username") String username, @QueryParam("password")String password)
    {
        User user = new User ();
        return user;
    }//end authenticate method


    @GET
    @Path ("/Subscriber")
    @Produces (MediaType.APPLICATION_JSON)
    public Response getSubsriber (@QueryParam("email") String email)
    {
        EntityManager entityManager = EMF.getInstance ().createEntityManager();

        try
        {
            Query query = entityManager.createQuery("SELECT FROM Subscriber WHERE email= :emailParam");
            query.setParameter("emailParam", email);

            Subscriber subscriber = (Subscriber)query.getSingleResult();
            return Response.status(Status.OK).entity(subscriber).build ();
        }
        catch(Exception e)
        {
            throw new RuntimeException (e);
        }
    }//end getSubsriber method
}//end Login class

我的用户类是:

@Path ("/users")
public class Users {


    /**
     * Gets all User objects from datastore. Returns
     * NOT_FOUND HTTP Response status if no result
     * found and OK HTTP Response status if it does with
     * a list of User objects. 
     * 
     * URL: domain-name/services/users/all
     * 
     * @author Mario Dennis
     * @return Response
     */
    @GET
    @Path ("/all")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAllUsers ()
    {
        try
        {
            EntityManager entityManager = EMF.getInstance().createEntityManager();
            Query query = entityManager.createQuery("SELECT FROM User");

            @SuppressWarnings("unchecked")
            List<User> user = (List<User>) query.getSingleResult ();
            GenericEntity<List<User>> entity = new GenericEntity<List<User>>(user){};

            return Response.status(Status.OK).entity(entity).build ();
        }
        catch (NoResultException e)
        {
            return Response.status(Status.NOT_FOUND).build ();
        }
    }//end getAllUsers method



    /**
     * Gets User object with user name and password specified from
     * datastore. Returns NOT_FOUND HTTP Response status if no result
     * found and OK HTTP Response status if it is found and BAD_REQUEST
     * if parameter supplied is empty.
     * a list of User objects. 
     * 
     * URL: domain-name/services/users/{username}&{password}
     * 
     * @author Mario Dennis
     * @return Response
     */
    @GET
    @Produces (MediaType.APPLICATION_JSON)
    public Response getUserByUsernamePassword (@QueryParam("username")String username,@QueryParam("password") String password)
    {
        /*
         * Sends back a BAD_REQUEST HTTP status
         * if email query parameter is empty
         */
        if (username.isEmpty() || password.isEmpty())
            return Response.status(Status.BAD_REQUEST).build ();

        try
        {
            EntityManager entityManager = EMF.getInstance().createEntityManager ();
            Query query = entityManager.createQuery ("SELECT FROM User WHERE user_name= :usernameParam AND password= :passwordParam");
            query.setParameter("usernameParam", username);
            query.setParameter("passwordParam",password);
            User user = (User) query.getSingleResult ();

            return Response.status(Status.OK).entity(user).build ();
        }
        catch (NoResultException e)
        {
            return Response.status(Status.NOT_FOUND).build ();
        }
    }//end getUserByUsernamePassword method



    /**
     * Gets User object with id specified from datastore. 
     * Returns NOT_FOUND HTTP Response status if no result
     * found and OK HTTP Response status if it is found and 
     * BAD_REQUEST if parameter supplied is empty.
     * a list of User objects. 
     * 
     * URL: domain-name/services/users/{id}
     * 
     * @author Mario Dennis
     * @return Response
     */
    @GET
    @Produces (MediaType.APPLICATION_JSON)
    public Response getUserById (@QueryParam("id")Long id)
    {
        /*
         * Sends back a BAD_REQUEST HTTP status
         * if email query parameter is empty.
         */
        if (id == null)
            return Response.status(Status.BAD_REQUEST).build ();

        try
        {
            EntityManager entityManager = EMF.getInstance ().createEntityManager ();
            User user = entityManager.find(User.class,id);

            /*
             * Checks if user was found. Return a NOT_FOUND
             * HTTP status if user was not located. 
             */
            if (user == null)
                return Response.status(Status.NOT_FOUND).build ();

            else
                return Response.status(Status.OK).entity(user).build();
        }
        catch (NoResultException e)
        {
            return Response.status(Status.NOT_FOUND).build ();
        }
    }//end getUserById method




    /**
     * Save User object to database. Return NOT_MODIFIED
     * HTTP status if persisting of object failed. Otherwise
     * return OK HTTP status if it was successful.
     * 
     * URL: domain-name/services/users/{JSON}
     * 
     * @author Mario Dennis
     * @param user
     * @return Response
     */
    @POST
    @Consumes  (MediaType.APPLICATION_JSON)
    public Response saveUser (User user)
    {
        try
        {
            EntityManager entityManager = EMF.getInstance().createEntityManager();
            entityManager.getTransaction ().begin();
            entityManager.persist(user);
            entityManager.getTransaction().commit();

            return Response.status(Status.OK).build ();
        }
        catch(Exception e)
        {
            return Response.status(Status.NOT_MODIFIED).build ();
        }
    }//end saveUser method

}//end Users class

当我删除用户类并上传回来时,其他两个服务又开始工作了。这是什么原因造成的?web.xml

 <!-- Jersey Framework mapping -->
   <servlet>
        <servlet-name>RESTful</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
          <param-name>com.sun.jersey.config.property.packages</param-name>
          <param-value>com.ppsjamaica.webservices</param-value>
        </init-param>
  </servlet>
  <servlet-mapping>
        <servlet-name>RESTful</servlet-name>
        <url-pattern>/service/*</url-pattern>
  </servlet-mapping>
    <!-- end Jersey Framework mapping -->
4

2 回答 2

4

请永远不要设计要求用户在 URI 中输入密码的服务。

On why it is failing: both methods use the same URI template (note the fact that each of them injects different query parameters does not mean they are mapped to a different URI - it is the value of @Path annotation that matters - and it is the same for both). You have to disambiguate them by mapping them to a different URI template.

于 2012-10-16T14:49:44.237 回答
0

方法是创建一个继承自的自定义异常WebApplicationException;您可以在此处查看更多详细信息。您不应该直接抛出 a RuntimeException,因为容器不知道它是否是预期的行为。

另一种方法是注释您的自定义异常(直接继承自RuntimeException),@ApplicationException尽管我不知道这是否是在 JAX-RS 中执行此操作的方法;仅在 EJB 中。

于 2012-10-15T17:57:25.847 回答