在Spring Boot中,参数之间有什么区别?

huangapple go评论75阅读模式
英文:

What is the difference between parameters in spring boot?

问题

我需要在控制器的@RequestMapping中处理不同类型的参数。例如,你如何区分以下路径:

/posts
/posts/1
/posts&userId=1

前两者之间似乎可以工作,但调用第三者会导致“模糊映射”错误。

以下是控制器代码:

@RequestMapping(value= {"/posts", "/posts/{numberOfPosts}"})
public String getBlogPosts(@PathVariable Optional<Integer> numberOfPosts) {	//为了防止“模糊映射”错误
	if (numberOfPosts.isPresent()) {
		return blogService.getUserBlogPosts(numberOfPosts);
	}
	else {
    	return blogService.getAllBlogPosts();
	}
}

以下是第三者:

@RequestMapping("/posts")
public String getUserIdPosts(@RequestParam int userId) {
	return blogService.getUserIdPosts(userId);
}

我该如何处理第三个问题?

英文:

I'm required to handle different types of parameters in the @RequestMapping of the controller. For example, how do you differentiate between

/posts
/posts/1
/posts&amp;userId=1

This seems to work between the first two but calling the third one results in "Ambiguous mapping" error.

Here is the controller code:

@RequestMapping(value= {&quot;/posts&quot;, &quot;/posts/{numberOfPosts}&quot;})
public String getBlogPosts(@PathVariable Optional&lt;Integer&gt; numberOfPosts) {	//to prevent &quot;Ambiguous mapping&quot; error
	if (numberOfPosts.isPresent()) {
		return blogService.getUserBlogPosts(numberOfPosts);
	}
	else {
    	return blogService.getAllBlogPosts();
	}
}

Here is the third one:

@RequestMapping(&quot;/posts&quot;)
public String getUserIdPosts(@RequestParam int userId) {
	return blogService.getUserIdPosts(userId);
	//return blogService.getUserBlogPosts(numberOfPosts);
}

How do I handle the third one?

答案1

得分: 0

/path/post&userId=1 的路径映射是相同的:/post`。

您可以通过在代码中对 userId 变量进行测试来区分这两个 URL,示例如下:

@RequestMapping(value= {"/posts", "/posts/{numberOfPosts}"}, method = RequestMethod.GET)
public String getBlogPosts(@PathVariable(required = false) Optional<Integer> numberOfPosts, @RequestParam(required = false) Integer userId) {
    if (numberOfPosts.isPresent()) {
        return blogService.getUserBlogPosts(numberOfPosts);
    }
    else if(userId == null) {
        return blogService.getAllBlogPosts();
    } else {
        return blogService.getUserIdPosts(userId);
    }
}

别忘了通过在上述注解中添加 required = false,使请求的参数 userId 和路径变量 numberOfPosts 成为可选项。

英文:

The path mapping for /post and /post&amp;userId=1 are the same: /post.

You can handle make the difference between these two URLs in your code by testing on the userId variable like below:

@RequestMapping(value= {&quot;/posts&quot;, &quot;/posts/{numberOfPosts}&quot;}, method = RequestMethod.GET)
public String getBlogPosts(@PathVariable(required = false) Optional&lt;Integer&gt; numberOfPosts, @RequestParam(required = false) Integer userId) {
    if (numberOfPosts.isPresent()) {
        return blogService.getUserBlogPosts(numberOfPosts);
    }
    else if(userId == null) {
        return blogService.getAllBlogPosts();
    } else {
        return blogService.getUserIdPosts(userId);
    }
}

Don't forger to make the requested parameter userId and the path variable numberOfPosts optional by adding required = false to the above annotations.

huangapple
  • 本文由 发表于 2020年10月1日 18:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64153976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定