我开发了一个消息传递EJB应用程序及其使用WILDFLY 9服务器的服务。 现在应用程序正在运行,数据库功能齐全,我正在尝试为此应用程序开发REST Web服务。所以我创建了我的RestActivator:
package tn.carmate.webservice;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("rest")
public class RestActivator extends Application {
}
和我的messageRessources.java如下
package tn.carmate.webservice;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import tn.esprit.pi.Services.Services;
import tn.esprit.pi.persistance.Message;
@Path("message")
@RequestScoped
public class MessageRessource {
@EJB
Services servicemessage;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{idreceiver}")
public Response getMessageList(@QueryParam(value = "idreceiver") int idReceiver) {
List<Message> message = servicemessage.getMessageList(idReceiver);
return Response.status(Status.OK).entity(message).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("{idSrc/idDest/content}")
public Response sendMessage(@QueryParam(value = "idSrc") int idSrc, @QueryParam(value = "idDest") int idDest,
@QueryParam(value = "content") String content) {
servicemessage.sendMessage(idSrc, idDest, content);
return Response.status(Status.OK).build();
}
@DELETE
@Path("{idm}")
public Response deleteMessage(@QueryParam(value = "idm") int id) {
servicemessage.deleteMessage(id);
return Response.status(Status.OK).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{idSender/idReceiver}")
public Response getConversation(@QueryParam(value = "idSender") int idSender,
@QueryParam(value = "idReceiver") int idReceiver) {
List<Message> conversion = servicemessage.getConversation(idSender, idReceiver);
return Response.status(Status.OK).entity(conversion).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{idR}")
public Response returnInbox (@QueryParam(value = "idR")int idR){
List<Message> listmessage = servicemessage.returnInbox(idR);
return Response.status(Status.OK).entity(listmessage).build();
}
@DELETE
@Path("{idc}")
public Response deleteConversation(@QueryParam(value = "idc")int id){
servicemessage.deleteConversation(id);
return Response.status(Status.OK).build();
}
}
但每当我尝试使用像这样的服务时,我都会收到此错误:
这是消息实体
package tn.esprit.pi.persistance;
import java.io.Serializable;
import java.lang.String;
import java.util.Date;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Message
*
*/
@Entity(name="message")
public class Message implements Serializable {
/* @EmbeddedId
private MessageId Id;
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String object;
private String content;
@Temporal(TemporalType.TIMESTAMP)
private Date dateEmission;
@ManyToOne
private User sender;
@ManyToOne
private User receiver;
private static final long serialVersionUID = 1L;
public Message(User src, User dest, String content) {
this.sender = src;
this.receiver = dest;
this.content = content;
this.dateEmission = new Date();
}
public Message() {
super();
}
/* public MessageId getId() {
return Id;
}public void setId(MessageId id) {
Id = id;
}
*/
public int getIdMessage() {
return this.id;
}
public String getObject() {
return this.object;
}
public void setObject(String object) {
this.object = object;
}
public User getReceiver() {
return this.receiver;
}
public void setReceiver(User reciver) {
this.receiver = reciver;
}
public User getSender() {
return this.sender;
}
public void setSender(User sender) {
this.sender = sender;
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDateEmission() {
return this.dateEmission;
}
public void setDateEmission(Date dateEmission) {
this.dateEmission = dateEmission;
}
}
答案 0 :(得分:2)
仔细检查您的网址,我认为应该是
http://localhost:8080
不是http://localhost:18080
。只是一个错误的错误。
其次,将restful服务的应用程序基本路径设置为rest
所以网址应该是
的 http://localhost:8080/CarMate-web/rest/message?idreceiver=1
强>
最后,idreceiver
是查询参数,但@Path(“{idreceiver}”)将其视为路径参数。
所以,在http://localhost:8080/CarMate-web/rest/message?idreceiver=1
Rest端点期望在/ message / {idreceiver}?idreceiver = 1之后的路径变量,如@Leonardo
在评论中所述。
我会建议以下列出的任何解决方案:
从方法
中删除@Path@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMessageList(@QueryParam(value = "idreceiver") int idReceiver) {
List<Message> message = servicemessage.getMessageList(idReceiver);
return Response.status(Status.OK).entity(message).build();
}
并使用以下网址:http://localhost:8080/CarMate-web/rest/message?idreceiver=1
或将@Path(“{idreceiver}”)更改为@Path(“idreceiver”)
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("idreceiver")
public Response getMessageList(@QueryParam(value = "idreceiver") int idReceiver) {
List<Message> message = servicemessage.getMessageList(idReceiver);
return Response.status(Status.OK).entity(message).build();
}
并使用以下网址:http://localhost:8080/CarMate-web/rest/message/idreceiver?idreceiver=1