需要你的帮助!当我在特定的URL生成JSON格式时。我从我的应用程序获得了JSON格式的所有数据。例如,我不是只生成帖子信息,而是一次性获得getMessageByID和getAllMessage ......
"网址:http://localhost:8080/Messenger/rest/messenger/messeger/i%20love%20running/Maks/Burkov/32059087"
为什么要生成所有信息? 这是我得到的:
如果只生成@Post信息,我需要做什么?
{
"name": "Maks",
"lastname": "Burkov",
"meessegaDate": 1461776715593,
"id_messenger": 32059087,
I need only the 4 lines above!
"message": "i love running",
"allmessages": [
"i love running"
]
}
@Path("/messeger")
public class MessageResource {
@POST
@Path("/{message}/{messager_name}/{messager_last_name}/{messager_id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MessengerDatabase ddMessage(@PathParam("message") String message, @PathParam("messager_name")String messager_name, @PathParam("messager_last_name")String messager_last_name ,@PathParam("messager_id") long messager_id){
MessengerDatabase mes = new MessageService().addMessage(message, messager_name, messager_last_name, messager_id);
return mes;
}
@GET
@Path("/{messager_id}")
@Produces(MediaType.APPLICATION_JSON)
public String GetMessageById(@PathParam("messager_id") long id){
String message = new MessageService().getMessageById(id);
System.out.println(message);
return message;
}
@GET
@Path("/allmess")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getAllMessages(){
return new MessageService().getAllMessages();
}
}
Messenger数据库:
public class MessengerDatabase {
public MessengerDatabase() {}
public MessengerDatabase(String message, long id){
setMessageByid(message, id);
}
public MessengerDatabase(String message, String messager_name, String messager_lastname,long messager_id) {
Date date_generator = new Date();
setMeessegaDate(date_generator);
setId_messenger(messager_id);
setMessage(message);
setMessageByid(message,messager_id);
setLastname(messager_lastname);
setName(messager_name);
System.out.println("In Constuctor");
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
private void setLastname(String lastname) {
this.lastname = lastname;
}
public Date getMeessegaDate() {
return meessegaDate;
}
private void setMeessegaDate(Date meessegaDate) {
this.meessegaDate = meessegaDate;
}
public long getId_messenger() {
return id_messenger;
}
private void setId_messenger(long id_messenger) {
this.id_messenger = id_messenger;
}
public String getMessage() {
return message;
}
private void setMessage(String message) {
this.message = message;
}
private void setMessageByid(String message, long id){
allmessages.add(message);
message_by_messenger_id.put(id,message);
}
@Override
public String toString() {
return "MessengerDatabase [name=" + name + ", lastname=" + lastname
+ ", meessegaDate=" + meessegaDate + ", id_messenger=" + id_messenger + ", message=" + message + "]";
}
public List<String> getAllmessages() {
return allmessages;
}
public String getMessage_by_messeger_id(long id) {
String messageById = message_by_messenger_id.get(id);
return messageById;
}
private static Map<Long,String> message_by_messenger_id = new HashMap<>();
private static List<String> allmessages = new ArrayList<>();
private String name;
private String lastname;
private Date meessegaDate;
private long id_messenger;
private String message;
web xml:
<display-name>Messenger</display-name>
<servlet>
<servlet-name>MessengerTest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>messenger.resources</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MessengerTest</servlet-name>
<url-pattern>/rest/messenger/*</url-pattern>
</servlet-mapping>
答案 0 :(得分:0)
为什么你的帖子有这条路?
@Path("/{message}/{messager_name}/{messager_last_name}/{messager_id}")
我会这样做:
@Path("/message")
@Consumes(MediaType.APPLICATION_JSON)
public MessengerDatabase ddMessage(Message message){
MessengerDatabase mes = new MessageService().addMessage(message);
return mes;
我认为你的路径相互冲突...在路径参数中发布变量并不是最好的设计......
这里有一个链接,可以帮助您重新设计API,如果您愿意: http://blog.octo.com/wp-content/uploads/2014/12/OCTO-Refcard_API_Design_EN_3.0.pdf
答案 1 :(得分:0)
您的POST方法正在返回MessengerDatabase。
@POST
@Path("/{message}/{messager_name}/{messager_last_name}/{messager_id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MessengerDatabase ddMessage(@PathParam("message") String message, @PathParam("messager_name")String messager_name, @PathParam("messager_last_name")String messager_last_name ,@PathParam("messager_id") long messager_id){
MessengerDatabase mes = new MessageService().addMessage(message, messager_name, messager_last_name, messager_id);
return mes;
}
使用配置的JSON编写器将此类转换为JSON。该JSON编写器通常会在MessengerDatabase对象中获取所有可读的JavaBeans属性,并将它们序列化为JSON。 MessengerDatabase类具有在每个JavaBeans约定定义的输出中接收的所有属性 - no-arg getters()。我在下面添加了相应的getter。
{
"name": "Maks", ==> getName()
"lastname": "Burkov", ==> getLastname()
"meessegaDate": 1461776715593, ==> getMeessegaDate()
"id_messenger": 32059087, ==> getId_messenger()
I need only the 4 lines above!
"message": "i love running", ==> getMessage()
"allmessages": [ ==> getAllmessages()
"i love running"
]
}
getMessage()和getAllmessages()会将它们添加到输出中。如果您不想在输出中使用这些,则可以在没有这些属性的情况下定义DTO(另一个像POJO的MessengerDatabase),将MessengerDatabase实例映射到它并返回该DTO。
请注意
您可能想要重新审视使用静态变量的方式。