[英]All my Restful web services stopped working when I add new service
Ok I have a package with two restful web service class Login.class and Subscribe.class. 好的,我有一个包含两个宁静的Web服务类Login.class和Subscribe.class的包。 When I deploy them to the app engine and test them they work.
当我将它们部署到app引擎并测试它们时,它们可以工作。 Now I add a new service class name User s.class to the package now none of my service works when I invoke them I get a internal server error.
现在我将新的服务类名称User s.class添加到包中,现在当我调用它们时,我的服务都无效。我收到内部服务器错误。
com.sun.jersey.spi.inject.Errors processErrorMessages: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Producing media type conflict. 严重:产生媒体类型冲突。 The resource methods public javax.ws.rs.core.Response com.ppsjamaica.webservices.PPSUsers.getUserById(java.lang.Long) and public javax.ws.rs.core.Response com.ppsjamaica.webservices.PPSUsers.getUserByUsernamePassword(java.lang.String,java.lang.String) can produce the same media type
资源方法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$ErrorMessagesException at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170) com.sun.jersey.spi.inject.Errors com.sun.jersey.spi.inject.Errors.processErrorMessages上的$ ErrorMessagesException(Errors.java:170)
my Subscribe class is: 我的订阅课是:
@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
}
my login class is: 我的登录类是:
@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
and my users class is: 我的用户类是:
@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
When I delete the user class and upload back the other two services starts working again. 当我删除用户类并上传回来时,其他两个服务再次开始工作。 What is causing this?
是什么造成的? web.xml
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 -->
Please don't ever design a service that requires users to put password in the URI. 请不要设计要求用户在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). 关于它失败的原因:两种方法都使用相同的URI模板(请注意,每个方法都注入不同的查询参数并不意味着它们被映射到不同的URI - 重要的是
@Path
注释的值 - 它是两者都一样)。 You have to disambiguate them by mapping them to a different URI template. 您必须通过将它们映射到不同的URI模板来消除它们的歧义。
On approach is to create a custom exception that inherits from WebApplicationException
; 方法是创建一个继承自
WebApplicationException
的自定义异常; you can see more details here . 你可以在这里看到更多细节 。 You should not throw directly a
RuntimeException
because the container does not know if it is an expected behavior. 您不应该直接抛出
RuntimeException
因为容器不知道它是否是预期的行为。
Another approach is to annotate your custom exception (which inherits directly from RuntimeException
) with @ApplicationException
although I do not know if this is the way to do it in JAX-RS; 另一种方法是用
@ApplicationException
注释你的自定义异常(它直接从RuntimeException
继承),虽然我不知道这是否是在JAX-RS中这样做的方法; only in EJB. 仅在EJB中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.