在 Spring MVC 休息服务上,我在尝试匹配超出我配置的 RequestMapping 值的任何内容时遇到问题。
所以例如我有这个:
@RequestMapping(value = "{configKey}/{arguments:.*}", method = RequestMethod.GET)
Run Code Online (Sandbox Code Playgroud)
这表示匹配超出第二个路径变量的任何内容。问题是这例如适用于:
get("/test/document")
Run Code Online (Sandbox Code Playgroud)
虽然这以 404 结束:
get("/test/document/download")
Run Code Online (Sandbox Code Playgroud)
奇怪的是,Spring 无法处理这个正则表达式。我实际上尝试了很多解决方案,但都没有奏效。
以前我在 JAX-RS 上有这个配置:
@Path("/{configKey}/{arguments:.*}")
Run Code Online (Sandbox Code Playgroud)
一切都很好,但现在我正在迁移并遇到这个问题。
有谁知道发生了什么以及如何解决这个问题?
编辑:
添加{configKey}/**- 不起作用
添加{configKey}/{arguments}/**作品,但例如,如果我打电话:
get("/test/document/download")我只test作为我的配置键和document参数。在争论中,我希望获得{configKey}. 这可以是任何东西,例如它在任何情况下都应该工作:
get("/test/document")
get("/test/document/download")
get("/test/document/download/1")
get("/test/document/download/1/2")
get("/test/whatever/xxx/1/2/etc")
Run Code Online (Sandbox Code Playgroud)
正在使用 JAX-RS 的配置: @Path("/{configKey}/{arguments:.*}")
请考虑以下示例:
@Controller
@RequestMapping({"/home"})
public class Home {
@RequestMapping(value = { "", "/" })
public String index() {
return "home";
}
@RequestMapping(value = { "-of-{id}" })
public String of(@PathVariable("id") String id) {
System.out.println(id);
return "home";
}
}
Run Code Online (Sandbox Code Playgroud)
index()完美地映射到'/ home'和'/ home /'; 但当(我想)将它映射到'/ home-of-{id}'时,(id)被映射到'/ home/-of- {id}'.
Spring会自动在'/ home'和'-of- {id}'之间添加斜杠,但我想消除它,有什么建议吗?
我有以下情况:
我的 REST API 一:
@RestController
@RequestMapping("/controller1")
Public Class Controller1{
@RequestMapping(method = RequestMethod.POST)
public void process(@RequestBody String jsonString) throws InterruptedException, ExecutionException
{
............
}
}
Run Code Online (Sandbox Code Playgroud)
REST API(Controller1) 的 JSON POST 请求 request1:
{
"key1":"value1",
"key2":"value2"
}
Run Code Online (Sandbox Code Playgroud)
我的 REST API 两个:
@RestController
@RequestMapping("/controller2")
Public Class Controller2{
@RequestMapping(method = RequestMethod.POST)
public void process(@RequestBody String jsonString) throws InterruptedException, ExecutionException
{
............
}
}
Run Code Online (Sandbox Code Playgroud)
REST API(Controller2) 的 JSON 请求 request2:
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
Run Code Online (Sandbox Code Playgroud)
我有几个这样的“原始”请求。现在,我期待一个 JSON 请求,我们称之为 request3,它是这种“原始”查询的组合 - 如下所示:
{
{
"requestType":"requestType1", …Run Code Online (Sandbox Code Playgroud) 我不明白这一点...我正在尝试捕获一个java.net.ConnectException以防我的下游 API 离线。然而 Eclipse 警告我它无法访问 - 提示代码不能抛出ConnectException. 然而,它显然可以。
@RequestMapping("/product/{id}")
public JsonNode getProduct(@PathVariable("id") int productID, HttpServletResponse oHTTPResponse)
{
RestTemplate oRESTTemplate = new RestTemplate();
ObjectMapper oObjMapper = new ObjectMapper();
JsonNode oResponseRoot = oObjMapper.createObjectNode();
try
{
ResponseEntity<String> oHTTPResponseEntity = oRESTTemplate.getForEntity("http://localhost:9000/product/"+productID, String.class);
}
catch (ConnectException e)
{
System.out.println("ConnectException caught. Downstream dependency is offline");
}
catch (Exception e)
{
System.out.println("Other Exception caught: " + e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
捕获的异常是:
Other Exception caught: I/O error on GET request for "http://localhost:9000/product/9": Connection refused: connect; …Run Code Online (Sandbox Code Playgroud) 我想在我的 Spring Boot 应用程序中创建一个自定义注释,它总是向我的类级别 RequestMapping path添加前缀。
我的控制器:
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;
@MyApi("/users")
public class UserController {
@GetMapping("/stackoverflow")
public String get() {
return "Best users";
}
}
Run Code Online (Sandbox Code Playgroud)
我的自定义注释
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RestController
@RequestMapping(path = "/api")
public @interface MyApi {
@AliasFor(annotation = RequestMapping.class)
String value();
}
Run Code Online (Sandbox Code Playgroud)
目标:最终的映射如下:/api/users/stackoverflow
笔记:
server.servlet.context-path不是一个选项,因为我想创建其中几个我有一个 Spring Boot REST 控制器端点,它接受 GET 和 POST 请求:
@RequestMapping(
value="/users",
method= {RequestMethod.GET, RequestMethod.POST},
headers= {"content-type=application/json"}
)
public ResponseEntity<List<User>> getUsers() {
if(/*Method is GET*/) {
System.out.println("This is a GET request response.");
} else if( /*Method is POST*/) {
System.out.println("This is a POST request response.");
}
}
Run Code Online (Sandbox Code Playgroud)
如果此端点被 GET 请求命中,我希望控制器在适当的 if 语句中执行某些操作。然而,如果端点被 POST 请求击中,我希望控制器采取另一种行动。
如何从休息控制器中提取这一信息?我宁愿不必将此共享端点拆分为两种不同的方法。这看起来很简单,我只是找不到任何相关文档。
我喜欢将所有映射保存在同一个地方,所以我使用XML配置:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/video/**=videoControllerr
/blog/**=blogController
</value>
</property>
<property name="alwaysUseFullPath">
<value>true</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
如果我在不同的控制器中创建具有相同名称的第二个请求映射,
@Controller
public class BlogController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info(@RequestParam("t") String type) {
// Stuff
}
}
@Controller
public class VideoController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info() {
// Stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
Caused by: java.lang.IllegalStateException: Cannot map handler 'videoController' to URL path [/info]: There is already handler of type [class com.cyc.cycbiz.controller.BlogController] mapped.
Run Code Online (Sandbox Code Playgroud)
有没有办法在不同的控制器中使用相同的请求映射?
我想要2个网址:
/video/info.html …Run Code Online (Sandbox Code Playgroud) 我希望将所有请求与一个不以'api'开头的PathVariable匹配.我在RequestMapping之后测试.spring可以匹配请求,但无法获得PathVariable的值.我怎么解决这个问题?
@RequestMapping(value = "/{name:(?!api).+}", method = RequestMethod.GET)
public void getNotApi(@PathVariable String name, HttpServletResponse response) {
...
}
Run Code Online (Sandbox Code Playgroud)
我收到一些请求的消息如下localhost:8080/resource.
Error 500 Missing URI template variable 'name' for method parameter of type String
我不得不从发送两个数据thymeleaf,以controller这样的a th:href:
<table id="itemTable" class="deneme">
<tbody>
<tr th:each="item : ${list.items}">
<td>
<p th:text="${item.content}"/>
<a th:href="@{/deleteItem/{listId}(listId=${list.id})/{itemId}(itemId=${item.id})}">
<span>Delet??e</span>
</a>
</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
控制器是:
@RequestMapping("/deleteItem/{listId}/{itemId}")
public String deleteItem(Model model, @PathVariable(value = "listId") Integer listId, @PathVariable(value = "itemId") int itemId) {
...
return "list";
}
Run Code Online (Sandbox Code Playgroud)
itemId带着真正的价值listId而来,但作为{listId}(listId=${toDoList.id})
究竟是什么问题?请帮我!
我正在使用带有@PreAuthorize 的自定义访问检查器:
@RestController
@RequestMapping("/users")
public class Users {
@PreAuthorize("@customAccessChecker.hasAccessToMethod('USERS', 'GET')")
@RequestMapping(method = RequestMethod.GET)
User getUsers() {
...
}
@PreAuthorize("@customAccessChecker.hasAccessToMethod('USERS', 'POST')")
@RequestMapping(method = RequestMethod.POST)
User addUser() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想去掉@PreAuthorize 注释中的字符串“GET”和“POST”。是否可以将@RequestMapping 中使用的 RequestMethod 以某种方式作为 hasAccessToMethod 的变量输入?
java spring spring-annotations spring-restcontroller request-mapping
request-mapping ×10
spring ×9
java ×5
rest ×4
spring-mvc ×3
spring-boot ×2
json ×1
resttemplate ×1
slash ×1
thymeleaf ×1