JAVA API,JERSEY/POST 不工作

标签 java api jersey

所以我的代码中有 POST 方法:

 @POST
      @Path("/send/{userPost}")
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces("application/json")
          public Response sendUser(@PathParam("userPost") String userPost ) {
           List<Post>userPosts = new ArrayList();
            Post post = new Post(99,userPost,"Bartek Szlapa");
            userPosts.add(post);
            User user = new User(99,"Bartek","Szlapa",userPosts);

              String output = user.toString();
              return Response.status(200).entity(output).build();

          }

不幸的是它不工作。我收到 404 错误。服务器配置正确,因为其他方法运行良好。有趣的是,当我删除 {userPost} 时,参数:@PathParam("userPost") String userPost 并发送空请求:http://localhost:8080/JavaAPI/rest/api/send它有效 - 我在某些字段中获得了带有 null 的新用户对象。你知道为什么我不能发送参数吗?在此先感谢您的帮助! :)

最佳答案

您发送的不是基于您的 api 将您的值作为路径参数发送的路径参数,假设您正在尝试发送“测试”

http://localhost:8080/JavaAPI/rest/api/send/test

如果你想使用查询参数

@POST
  @Path("/send")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces("application/json")
      public Response sendUser(@QueryParam("userPost") String userPost ) {

你的要求应该是

http://localhost:8080/JavaAPI/rest/api/send?userPost=test

关于JAVA API,JERSEY/POST 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033465/

相关文章:

java - 如何使用 Delphi 提取 jar 文件中定义的符号?

python - 帮助获取 Reddit 帖子链接到的 URL With PRAW

java - Android kitkat API 19,Intent.ACTION_VIEW 不起作用

rest - 找不到提供程序类 : when running Jersey REST example application

java - Jacob Com Outlook 使用 Dispatch.call 发送邮件

Java URLDecoder 将百分比符号解码为错误

java - 不活动期后连接超时问题

api - 从移动/网络应用向后端api发送每个请求的用户名和密码是否是个坏主意

java - 在java中使用静态类时

java - Jersey/JAXB 将不同的 JSON 格式绑定(bind)到单一类型的 java 对象