我正在尝试构建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
这与这个问题类似,但我仍然对我的情况感到困惑.我想将这种蚂蚁风格的模式映射到控制器方法:
/results/**
Run Code Online (Sandbox Code Playgroud)
也就是说,我希望任何URL www.hostname.com/MyServlet/results/123/abc/456/def/都可以使用此方法.我有:
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/results/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
和:
@RequestMapping(value="/**", method=RequestMethod.GET)
public ModelAndView handleRequest() {...}
Run Code Online (Sandbox Code Playgroud)
这可以指导我的方法请求,但引出了几个问题:
<url-pattern>/another-mapping/*</url-pattern>??? 它也会映射到该方法!我该如何将两者分开?/results/*工作,而/results/**不是?根据ant路径样式,**意味着包含嵌套/字符,而*在下一个停止/.因此,它应该只能成功映射一个URL,如/results/123bot not /results/123/abc/.对?我在春天value = "/redirect/{id}"的@RequestMapping注释中一直看到这种格拉姆.我一直想知道{id}这里有什么?这是某种Expression Language吗?
我看到的示例代码:
@RequestMapping( value = "/files/{id}", method = RequestMethod.GET )
public void getFile( @PathVariable( "id" )
String fileName, HttpServletResponse response )
{
try
{
// get your file as InputStream
InputStream is = new FileInputStream("/pathToFile/"+ fileName);
// copy it to response's OutputStream
IOUtils.copy( is, response.getOutputStream() );
response.flushBuffer();
}
catch( IOException ex )
{
throw new RuntimeException( "IOError writing file to output stream" );
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是{id}映射中的内容是什么,它与@PathVariable …
我有一个控制器,请求映射@RequestMapping("/**")
为什么意思?
如果我想从上面的映射中排除某些url模式,我该怎么做?
有人可以对此有所了解吗?
我尝试使用如下代码:
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Brand getBrand(@PathVariable Integer id) {
return brandService.getOne(id);
}
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public List<Brand> getBrand(@PathVariable String name) {
return brandService.getSome(name);
}
Run Code Online (Sandbox Code Playgroud)
但我得到这样的错误,我该怎么办?
java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/api/brand/1': {public java.util.List com.zangland.controller.BrandController.getBrand(java.lang.String), public com.zangland.entity.Brand com.zangland.controller.BrandController.getBrand(java.lang.Integer)}
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:375) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
Run Code Online (Sandbox Code Playgroud) 我知道在基于Spring MVC的应用程序中使用的@RequestMapping注释.
我遇到了这段代码:
@RequestMapping(method = POST, params = {"someParam"})
Run Code Online (Sandbox Code Playgroud)
我理解了method.但是我不知道是什么params意思?在此之前,我从来没有见过任何通过params到这个注释的东西.
任何人都可以帮助理解这个吗?
我们的一个应用程序泄漏文件句柄,我们还没有找到原因.
在代码中我可以看到几个与此类似的函数:
public ResponseEntity<InputStreamResource> getFoo( ... ) {
InputStream content = getContent(...)
InputStreamResource isr = new InputStreamResource(content);
return ResponseEntity.status(HttpServletResponse.SC_OK).body(isr);
}
Run Code Online (Sandbox Code Playgroud)
(为简洁而if检查和try/或catch删除)
我确信这一部分会导致问题,因为当我用JMeter加载测试这个特定代码时,我可以看到getContent()在这个阶段失败:
is = Files.newInputStream(f.toPath());
Run Code Online (Sandbox Code Playgroud)
通常我会关闭InputStream但是因为这个简短而简单的代码我无法在之前关闭流return或调用body.
当我运行lsof(代码在Linux上运行)时,我可以看到数千个文件在读取模式下打开.所以我确定这个问题是由于流没有关闭引起的.
我应该交易最佳实践代码吗?
我正在构建一个内部库,它应该自动向Spring MVC应用程序添加一些控制器.这些控制器都@RestController带有一些带注释的处理程序方法@RequestMapping.由于它是一个库,我希望用户能够指定库应该公开所有这些控制器的根路径.插图:
// given that I have a controller like this:
@RestController
@RequestMapping("/admin")
class AdminController {
@RequestMapping("/users")
UsersDto allUsers() { ... }
}
// it will be exposed at `http://localhost/admin/users`
Run Code Online (Sandbox Code Playgroud)
我想要实现的是使/admin部件可配置.例如,如果用户说application.properties:
super.admin.path=/top/secret/location/here
Run Code Online (Sandbox Code Playgroud)
我希望AdminController处理程序可用http://localhost/top/secret/location/here,因此allUsers()处理程序应该有一个完整的路径:
http://localhost/top/secret/location/here/users
Run Code Online (Sandbox Code Playgroud)
我该如何实现这一目标?感觉就像一个非常常见的任务,但我没有设法找到一个有效的简单解决方案.
有一种SimpleUrlHandlerMapping似乎正是我想要的机制:
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setOrder(Integer.MAX_VALUE - 2);
simpleUrlHandlerMapping.setUrlMap(Collections.singletonMap("/ANY_CUSTOM_VALUE_HERE/*", "adminController"));
return simpleUrlHandlerMapping;
}
Run Code Online (Sandbox Code Playgroud)
但它一直在说
DispatcherServlet配置需要包含支持此处理程序的HandlerAdapter
Spring Boot Admin项目具有这个确切的功能 - 您可以配置它们应该公开所有端点的位置.他们似乎在PrefixHandlerMapping中 …
我有WebDeviceInfo和IOSDeviceInfo类,它们是 的子类DeviceInfo。如何在 Spring 中创建一个@RestController接受 或 的IOSDeviceInfo单个端点WebDeviceInfo?
尝试#1
我尝试将其映射RequestMapping到两种不同的方法,一种方法是如果可以RequestBody映射到 a则调用WebDeviceInfo,另一种方法如果可以映射RequestBody到 a则调用IOSDeviceInfo。
@RequestMapping(value = "/register-device", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void registerWebDevice(@RequestBody final WebDeviceInfo webDeviceInfo) {
//register web device
}
@RequestMapping(value = "/register-device", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void registerIOSDevice(@RequestBody final IOSDeviceInfo iosDeviceInfo) {
//register ios device
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,第二个方法RequestMapping没有注册,并且应用程序无法启动,因为 Spring 看到了/register-device相同的方法RequestMethod …
在浏览了docs.spring.org引用的一些教程和初始文档后,我了解到它是在开发人员创建的POJO类的控制器中创建的.但在阅读本文时,我看到了以下段落:
一个
@ModelAttribute上的方法参数指示参数应该从模型中检索.如果模型中不存在,则应首先实例化参数,然后将其添加到模型中.一旦出现在模型中,参数的字段应该从具有匹配名称的所有请求参数中填充.这在Spring MVC中称为数据绑定,这是一种非常有用的机制,可以使您不必单独解析每个表单字段.Run Code Online (Sandbox Code Playgroud)@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST) public String processSubmit(@ModelAttribute Pet pet) { }
在该段中,最令人不安的是这条线:
"如果模型中没有......"
模型中的数据如何?(因为我们还没有创建模型 - 它将由我们创建.)
此外,我已经看到一些控制器方法接受该Model类型作为参数.那是什么意思?它是否Model在某处创建?如果是这样,谁为我们创造它?
request-mapping ×10
spring ×8
spring-mvc ×7
java ×4
rest ×2
spring-boot ×2
inheritance ×1
servlets ×1
spring-web ×1