Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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 春季休息的子资源_Java_Rest_Spring Mvc_Spring Boot_Request Mapping - Fatal编程技术网

Java 春季休息的子资源

Java 春季休息的子资源,java,rest,spring-mvc,spring-boot,request-mapping,Java,Rest,Spring Mvc,Spring Boot,Request Mapping,我正在尝试创建messanger应用程序 我必须从MessageResource调用CommentResource 我需要单独的MessageResources和CommentResources 我在做这样的事情: MessageResource.java @RestController @RequestMapping("/messages") public class MessageResource { MessageService messageService = new Mess

我正在尝试创建messanger应用程序

我必须从MessageResource调用CommentResource

我需要单独的MessageResources和CommentResources

我在做这样的事情:

MessageResource.java

@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return new CommentResource();
    }
}
@RestController
@RequestMapping("/")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    // as usual messages related CRUD operations
}
@RestController
@RequestMapping("messages/{messageId}/comments")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
CommentResource.java

@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return new CommentResource();
    }
}
@RestController
@RequestMapping("/")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    // as usual messages related CRUD operations
}
@RestController
@RequestMapping("messages/{messageId}/comments")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
我想要

返回“这是测试注释”

有什么想法吗

PS:简单地说,我想知道
JAX-RS子资源
spring rest中的等效实现

您的url()表明注释嵌套在消息中。您的控制器应如下所示:

@RestController
@RequestMapping("/messages")
public class MessageResource {

    @RequestMapping(value = "/{messageId}")
    public String getCommentResource(@PathVariable("messageId") String messageId) {
        //test
        return messageId;
    }

    @RequestMapping(value = "/{messageId}/comments/{commentsContent}")
    public String getCommentResource(
                      @PathVariable("messageId") String messageId, 
                      @PathVariable("commentsContent") String commentsContent) {
        //test
        return messageId + "/" + commentsContent;
    }
}
我不完全确定您希望在MessageResource类中执行什么操作,但想法就在这里

Rest-HTTP方法 目前,这些使用是Get请求。然而,您应该考虑使用适当的HTTP方法:

  • 获取:读取资源
  • Post:创建一个资源
  • Put:更新
  • 删除:删除:)
看看这个:

以Post为例:
您所寻找的内容在
JAX-RS
实现中得到支持,如
Sub-Resources
。在构建嵌套在自然界中的大型API时,子资源是非常有用的特性

Spring引导默认rest实现不是一个
JAX-RS
,而是
SpringMVC
。虽然可以在Spring Boot中使用Jersey,但它的设置有点复杂,在社区中似乎没有得到很好的使用/支持


顺便说一句,这真是太棒了

我还被迫从JAX-RS迁移到SpringMVC

我仍然在寻找一种优雅的方法来实现这一点,就像我使用JAX-RS一样

我在分享我的尝试

@RestController
@RequestMapping("parents")
public class ParentsController {

    @RequestMapping(method = RequestMethod.GET,
                    produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<List<Parent>> read() {
    }

    @RequestMapping(method = RequestMethod.GET,
                    path = "/{id:\\d+}",
                    produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<Parent> read(@PathVariable("id") final long id) {
    }
}
我发现了两个问题

@PathVariable
不适用于字段。 我就是不能做
@PathVariable(“parentId”)私有长parentId

对于
ChildrenController
JAX-RS的一个优点是我们可以为不同的路径映射
ChildrenController
,就像
ChildrenController
有一个类级别的
@Path

@Path("/children");
public class ChildrenResource {
}

@Path("/parents")
public class ParentsResource {

    @Path("/{id}/children")
    public ChildrenResource resourceChildren() {
    }
}


/children
/parents/{id}/children

你可以保持简单。只需创建两个类,并使用常量引用子资源和父资源。这有助于在两个类之间建立链接,并使开发人员了解它们之间的关系

因此:

以及:

您还可以将控制器拆分到不同的包中,并在包组织中显示此层次结构:

com.company.web.message.MessageController
com.company.web.message.comment.CommentController 

在Spring引导中,我们可以使用@AutowiredSpring概念实现JAX-RS子资源概念。创建子资源类的对象,Spring将在运行时初始化并返回该对象。不要手动创建对象。 比如: 上述情景

 - MessageResource.java

@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();
    @Autowired
    @Qualifier("comment")
    CommentResource comment;

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return comment;
    }
}    

 - CommentResource.java

@RestController("comment")
@RequestMapping("/")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}



Now it will work like sub-resource
http://localhost:8080/messages/1/comments/abc

You can send any type of request.

MessagesController.java

@RestController
@RequestMapping(value = "/messages")
public class MessageController {

    @Autowired
    private MessagesService messageService;

}
@RestController
@RequestMapping("/messages/{messageId}/comments")
public class CommentController {


    @GetMapping
    public List<Comment> getComments(@PathVariable("messageId") Long messageId) {
        System.out.println("Get "+messageId);

        return null;
    }

}
CommentController.java

@RestController
@RequestMapping(value = "/messages")
public class MessageController {

    @Autowired
    private MessagesService messageService;

}
@RestController
@RequestMapping("/messages/{messageId}/comments")
public class CommentController {


    @GetMapping
    public List<Comment> getComments(@PathVariable("messageId") Long messageId) {
        System.out.println("Get "+messageId);

        return null;
    }

}
@RestController
@请求映射(“/messages/{messageId}/comments”)
公共类控制器{
@GetMapping
公共列表getComments(@PathVariable(“messageId”)长messageId){
System.out.println(“Get”+messageId);
返回null;
}
}
MessageResource.java

@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return new CommentResource();
    }
}
@RestController
@RequestMapping("/")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    // as usual messages related CRUD operations
}
@RestController
@RequestMapping("messages/{messageId}/comments")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
CommentResource.java

@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return new CommentResource();
    }
}
@RestController
@RequestMapping("/")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    // as usual messages related CRUD operations
}
@RestController
@RequestMapping("messages/{messageId}/comments")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}

为什么要尝试从另一个控制器方法而不是某种值返回控制器?既然您希望此资源具有路径/messages/1/comments/abc,为什么要将其映射到/而不是/messages/{messageId}/comments?我想将CommentResource构建为MessageResource的子资源,并将CommentAPI保留在CommentResource中。使用MessageResource中的“/{messageId}/comments”,我想将所有请求重定向到CommentResource。如果我将/messages/{messageId}/comments/abc放入我的MessageResource类中,它就会工作。但我想把它分开,因为/abc与评论相关,而不是消息。@prranay您有没有找到与Spring类似的方法?很明显,那些评论员不明白你想做什么。你的解决方案工作得很好,但我想把这两个控制器分开,不想合并。是的,它解决了问题,代码看起来很干净,因为MessageResource只包含消息API,而CommentResource包含评论API。下面是我正在使用的代码,您的解决方案为我研究这个问题提供了新的方向。谢谢:)@RestController@RequestMapping(“/messages/{messageId}/comments”)公共类CommentResource{/////会更好。由于注释始终是某些消息的一部分,并且您必须具有一些消息Id才能获取注释。因此尽可能多地使用公共uri路径到类级别更好。您没有回答他的问题。他要求提供JAX-RS子资源替代方案,您只是向他展示了如何提供REST服务ice a URL的工作方式很有魅力!遗憾的是,我在官方文档中找不到这样的示例。您是如何运行它的?它需要一些配置吗?在当前版本的spring boot(2.2.4.RELEASE)上它将CommentResource视为要序列化的响应值。如果我添加一个带有getter的字段,我会得到它的json表示。调用get时,我会得到404。我不知道如何理解这一点。首先,“public CommentResource getCommentResource()”返回CommentResource,但不会将messageId上下文注入其中或以任何方式使用它。因此,我不确定这是回答中的错误还是我的知识差距-请澄清。此外,注释资源的路径“Comment”与“comments”不同-例如单数vs.复数,所以我不确定这是否是故意的-我认为这是一个错误。感谢您的更正或回复。