Java 编程新手,仍在学习中。我已经构建了一个 RESTful 服务,并且尝试传入 GET 例程的参数,但我返回状态 400,表示“请求实体不能为空”。当我调用非参数化 GET 时,数据返回得很好。我已经剥离了参数化 GET 的所有功能,只返回一个简单的字符串,但我仍然收到相同的消息。搜索遍了,找不到任何有用的东西。
下面是我为该服务运行的代码。 “GetChildAllInfo”方法调用本地 mySQL 实例并返回对象列表;那一个工作得很好。参数化的不返回任何内容,甚至不返回异常。
任何帮助将不胜感激。即使这是一个极其简单的解决方案,例如我可能错过的语法错误。我也愿意接受您在代码中看到的任何其他建议。谢谢!
package allowanceManagerChild;
import java.util.Set;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
import com.google.gson.Gson;
@Path("allowanceManagerChild")
public class AllowanceManagerChild {
@Context
private UriInfo context;
/**
* Creates a new instance of AllowanceManagerChild
*/
public AllowanceManagerChild() {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getJson() {
String response = "";
Set<Child> children = Child.GetAllChildInfo();
for (Child child : children){
Gson gson = new Gson();
String json = gson.toJson(child);
response = response + json;
}
return response;
}
@GET
@Path("/{childID}")
@Produces(MediaType.APPLICATION_JSON)
public String getJson(int childID) {
String response = "";
try{
// Set<Child> children = Child.GetChildInfo(id);
// for (Child child : children){
// Gson gson = new Gson();
// String json = gson.toJson(child);
// response = response + json;
// }
response = "Made it here"; //Integer.toString(childID);
}
catch(Exception e){
response = e.toString();
}
return response;
}
/**
* PUT method for updating or creating an instance of AllowanceManagerChild
* @param content representation for the resource
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void putJson(String content) {
}
}
最佳答案
将 @PathParam
注释添加到方法参数可能会有所帮助:
@GET
@Path("/{childID}")
@Produces(MediaType.APPLICATION_JSON)
public String getJson(@PathParam("childID") int childID) {
请参阅RESTful Web Services Developer's Guide了解更多详情。
关于java - 从参数化 RESTful GET 操作接收 "Request Entity Cannot be Empty",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39339754/