Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “接收”;请求实体不能为空";从参数化RESTful GET操作_Java_Rest - Fatal编程技术网

Java “接收”;请求实体不能为空";从参数化RESTful GET操作

Java “接收”;请求实体不能为空";从参数化RESTful GET操作,java,rest,Java,Rest,java编程新手,仍在学习中。我已经构建了一个RESTful服务,我正在尝试为GET例程传递一个参数,我得到一个状态400,表示“请求实体不能为空”。当我调用非参数化GET时,数据返回得很好。我已经剥离了参数化GET的所有功能,只返回一个简单的字符串,我仍然得到相同的消息。搜遍了,没有找到任何有用的东西 下面是我为该服务运行的代码。方法“GetChildAllInfo”调用本地mySQL实例并返回对象列表;那只很好用。参数化的一个不返回任何内容,甚至不返回异常 任何帮助都将不胜感激。即使这是一个

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) {
        }
    }
包allowanceManagerChild;
导入java.util.Set;
导入javax.ws.rs.core.Context;
导入javax.ws.rs.core.UriInfo;
导入javax.ws.rs.products;
导入javax.ws.rs.Consumes;
导入javax.ws.rs.GET;
导入javax.ws.rs.Path;
导入javax.ws.rs.PUT;
导入javax.ws.rs.core.MediaType;
导入com.google.gson.gson;
@路径(“allowanceManagerChild”)
公共类AllowanceManagerChild{
@上下文
私有信息上下文;
/**
*创建AllowanceManagerChild的新实例
*/
公共AllowanceManagerChild(){
}
@得到
@产生(MediaType.APPLICATION_JSON)
公共字符串getJson(){
字符串响应=”;
Set children=Child.GetAllChildInfo();
用于(儿童:儿童){
Gson Gson=新的Gson();
字符串json=gson.toJson(子级);
response=response+json;
}
返回响应;
}
@得到
@路径(“/{childID}”)
@产生(MediaType.APPLICATION_JSON)
公共字符串getJson(int-childID){
字符串响应=”;
试一试{
//Set children=Child.GetChildInfo(id);
//用于(儿童:儿童){
//Gson Gson=新的Gson();
//字符串json=gson.toJson(子级);
//response=response+json;
//        }
response=“make it here”;//Integer.toString(childID);
}
捕获(例外e){
响应=e.toString();
}
返回响应;
}
/**
*用于更新或创建AllowanceManagerChild实例的PUT方法
*@param资源的内容表示形式
*/
@放
@使用(MediaType.APPLICATION_JSON)
public void putJson(字符串内容){
}
}

@PathParam
注释添加到方法参数可能会有所帮助:

@GET
@Path("/{childID}")
@Produces(MediaType.APPLICATION_JSON)
public String getJson(@PathParam("childID") int childID) {

有关更多详细信息,请参阅。

成功了!谢谢你的帮助@很高兴这有帮助!请将答案标记为“已接受”,这样问题就不会显示为“未回答”。