我是Jax-RS的新手。我正在尝试使用实现一个简单的GET方法球衣。我获得了Collection资源的正确输出,但我收到实例资源“ ERROR 400 Bad Request”。一世被困在这一点上。一切似乎都是正确的,但我是缺少我无法弄清楚的东西。我已经尝试过去10天。任何帮助将是可观的。请找到我的代码如下。我正在使用POSTMAN rest client“意外的'T'”作为响应,ARC抛出“请求实体不能为空。”,并且两者都是错误400错误的请求。
这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>MessangerWithoutMaven</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.manish.jax_rs.Messanger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
这是我的MessageResource.java:
package com.manish.jax_rs.Messanger.resources;
import java.util.List;
import javax.websocket.server.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.manish.jax_rs.Messanger.model.Message;
import com.manish.jax_rs.Messanger.service.MessageService;
@Path("/messages")
public class MessageResource {
MessageService messageService = new MessageService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Message> getMessages(){
return messageService.getAllMessages();
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Message getMessage(@PathParam("id") int id){
return messageService.getMessage(id);
}
}
这是DaoClass.java:
package com.manish.jax_rs.Messanger.Dao;
import java.util.HashMap;
import java.util.Map;
import com.manish.jax_rs.Messanger.model.Message;
public class DaoClass {
private static Map<Integer,Message> messages = new HashMap<Integer,Message>();
public static Map<Integer, Message> getMessages() {
return messages;
}
}
这是MessageService.java:
package com.manish.jax_rs.Messanger.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.manish.jax_rs.Messanger.Dao.DaoClass;
import com.manish.jax_rs.Messanger.model.Message;
public class MessageService {
private Map<Integer,Message> messages= DaoClass.getMessages();
Message m1= new Message(1,"Hello John","John");
Message m2= new Message(2,"Hello Mathews","Mathews");
Message m3= new Message(3,"Hello Albert","Albert");
public MessageService(){
messages.put(1,m1);
messages.put(2,m2);
messages.put(3, m3);
}
public List<Message> getAllMessages(){
return new ArrayList<Message>(messages.values());
}
public Message getMessage(int id){
return messages.get(id);
}
}
这是Message.java:
package com.manish.jax_rs.Messanger.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Message {
private int id;
private String message;
//private Date created;
private String author;
public Message() {
}
public Message(int id, String message, String author) {
this.id = id;
this.message = message;
this.author = author;
//this.created = new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
[List of jars][1]
[correct output for collection resource "messages" in Postman rest client][2]
[Incorrect output for instance resource /messages/1][3]enter code here
[1]: https://i.stack.imgur.com/sWfMl.jpg
[2]: https://i.stack.imgur.com/y5Nnf.jpg
[3]: https://i.stack.imgur.com/8CwId.jpg
请检查您导入的PathParam。代替“ javax.websocket.server.PathParam;”使用“导入javax.ws.rs.PathParam;”。这应该可以正常工作。
您已导入import javax.websocket.server.PathParam;应该是import javax.ws.rs.PathParam;