prr*_*nay 14 java rest spring-mvc spring-boot request-mapping
我正在尝试构建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();
}
}
Run Code Online (Sandbox Code Playgroud)
CommentResource.java
@RestController
@RequestMapping("/")
public class CommentResource {
private CommentService commentService = new CommentService();
@RequestMapping(method = RequestMethod.GET, value="/abc")
public String test2() {
return "this is test comment";
}
}
Run Code Online (Sandbox Code Playgroud)
我想要
HTTP://本地主机:8080 /消息/ 1 /评论/ ABC
返回"这是测试评论".
任何的想法??
PS:简单来说,我想知道JAX-RS sub-resource
相应的实现spring-rest
Tap*_*pan 17
在 Spring boot 中,我们可以使用@Autowired Spring Concept实现 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.
Run Code Online (Sandbox Code Playgroud)
您的网址(http:// localhost:8080/messages/1/comments/abc)表明评论嵌套在邮件中.您的控制器应如下所示:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我不完全确定你想在MessageResource类中做什么,但想法就在那里.
目前,这些用途是Get请求.但是,您应该考虑使用适当的Http方法:
看看这个:http://www.restapitutorial.com/lessons/httpmethods.html
@RequestMapping(method=RequestMethod.POST, value = "/{messageId}/comments/{commentsContent}")
public ResponseEntity<String> getCommentResource(
@PathVariable("messageId") String messageId,
@RequestBody Comment comment) {
//fetch the message associated with messageId
//add the comment to the message
//return success
return new ResponseEntity<String>(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
另外,我个人会将这些类重命名为MessageController和CommentController.
你可以简单地拆分控制器(更接近你所拥有的):
@RestController
@RequestMapping("/messages")
public class MessageResource {
@RequestMapping(value = "/{messageId}")
public String getCommentResource(@PathVariable("messageId") String messageId) {
//test
return messageId;
}
}
@RestController
@RequestMapping("/messages")
public class CommentResource {
@RequestMapping(value = "/{messageId}/comments/{commentsContent}")
public String getCommentResource(
@PathVariable("messageId") String messageId,
@PathVariable("commentsContent") String commentsContent) {
//test
return messageId + "/" + commentsContent;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您正在寻找的内容在Jersey等JAX-RS
实现中得到支持,并被称为. 在构建本质上嵌套的大型 API 时,子资源是一个非常有用的功能。Sub-Resources
Spring Boot 默认的 rest 实现不是一个JAX-RS
but SpringMVC
。虽然可以在 Spring Boot 中使用 Jersey,但尝试设置它有点复杂,并且在社区中似乎没有得到很好的使用/支持。
顺便说一句,DropWizard很棒!
归档时间: |
|
查看次数: |
9166 次 |
最近记录: |