正在开发简单的休息网络服务,并采用球衣框架。编写@POST方法,消耗并生成{`MediaType.APPLICATION_JSON}。使用chrome POSTMAN rest客户端来调用Web服务。 Webservices正如预期的那样运行以下application / json请求
{
"author": "vijay",
"created": "2016-10-28T14:55:55.273+05:37",
"message": "RestServicedsfdsfsdfsdf"
}
代码中没有编译错误。
消息资源:
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/messages")
public class MessageResource {
MessageService msgService = new MessageService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Message> getMessages() {
return msgService.getAllMessages();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{messageId}")
public Message getMessage(@PathParam("messageId")long messageId) {
return msgService.getMessage(messageId);
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Message addMessage(Message message) {
return msgService.addMessage(message);
}
@PUT
@Path("/{messageId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Message updateMesage(@PathParam ("messageId") long id, Message message) {
message.setId(id);
return msgService.updateMessage(message);
}
}
消息服务:
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MessageService {
private Map<Long, Message> messages = MesaageDataBase.getMessages();
public MessageService() {
messages.put(1L, new Message(1232, "RestService", "vijay"));
messages.put(2L, new Message(1233, "RestServiceClient", "Ashok"));
}
public List<Message> getAllMessages() {
return new ArrayList<Message>(messages.values());
}
public Message getMessage(long id) {
return messages.get(id);
}
public Message addMessage(Message message) {
message.setId(messages.size() +1);
messages.put(message.getId(), message);
return message;
}
public Message updateMessage(Message message) {
if(message.getId() <= 0) {
return null;
}
messages.put(message.getId(), message);
return message;
}
public Message removeMessage(Long id) {
return messages.remove(id);
}
}
消费输入数据我添加附件请找。 以下是我在chrome POSTMAN客户端中遇到的错误:
回应主体:
<html>
<head>
<title>Apache Tomcat/7.0.42 - Error report</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>HTTP Status 400 - Bad Request</h1>
<HR size="1" noshade="noshade">
<p>
<b>type</b> Status report
</p>
<p>
<b>message</b>
<u>Bad Request</u>
</p>
<p>
<b>description</b>
<u>The request sent by the client was syntactically incorrect.</u>
</p>
<HR size="1" noshade="noshade">
<h3>Apache Tomcat/7.0.42</h3>
</body>
</html>