Spring MVC #RequestMapping ... using method name as action value? - java

Say I have this:
#RequestMapping(value="/hello")
public ModelAndView hello(Model model){
System.out.println("HelloWorldAction.sayHello");
return null;
}
Is it possible to skip the value="hello" part, and just have the #RequestMapping annotation and have spring use the method name as the value, similar to this:
#RequestMapping
public ModelAndView hello(Model model){
System.out.println("HelloWorldAction.sayHello");
return null;
}
Thanks!
===================EDIT=====================
Tried this but not working:
#Controller
#RequestMapping(value="admin", method=RequestMethod.GET)
public class AdminController {
#RequestMapping
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}

Try to add "/*" on the request mapping value of the class
#Controller
#RequestMapping(value="admin/*")
public class AdminController {
#RequestMapping
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
You can go the page http://localhost:8080/website/admin/hello

It should work if you move the RequestMethod on your specific method:
#Controller
#RequestMapping(value="admin")
public class AdminController {
#RequestMapping(method=RequestMethod.GET)
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
and access it through http://hostname:port/admin/hello
Have a look here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping
Good luck

Related

how to add specific logic to the method by custom annotation in Spring boot?

My question is this.
I would like to add custom annotation to spring boot and designate it as declared without declaring a specific logic.
Suppose you have the following code:
#MyCustomAnnotation
#Controller
public class ExController {
#RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model){
return "index";
}
}
I want the above code to perform the following logic.
#MyCustomAnnotation
#Controller
public class ExController {
#RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model){
//Code added because it has an annotation
model.addAttribute("key","value");
//Code added because it has an annotation
return "index";
}
}
I've been thinking about Reflection and other methods, but I can't think of the right way
Can someone grateful give a solution or keyword for this problem?
#RequestParam is a Spring annotation that is used to bind a parameter to the value of a method parameter.
If you want to add logic to your method, you can do so without any annotations.
public ResponseEntity<String> myMethod(#RequestParam(required = false) String param) {
//additional logic
return new ResponseEntity<>(HttpStatus.OK);
}
The above code will add additional logic to the method.
i hope this can be helpful i am new to this .
You can add specific logic to the method by custom annotation in Spring boot by using the #Component annotation.
You can use #Configuration annotation to add your custom logic.
<code>#Configuration
public class MyCustomAnnotationConfig {
#Bean
public MyCustomAnnotationBean myCustomAnnotationBean() {
return new MyCustomAnnotationBean();
}
}
And then you can use #Autowired to inject your bean into your controller.
```
#MyCustomAnnotation
#Controller
public class ExController {
#Autowired
private MyCustomAnnotationBean myCustomAnnotationBean;
#RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model){
myCustomAnnotationBean.doSomething();
return "index";
}
}

get the request path in a spring controller

How can I get the matched request path in an HTTP mapping method in a spring application? In the following example, I want matchedPath variable to have the value /api/products/{product_id}/comments which is a combination of #RequestMapping and #PostMapping value.
#RestController
#RequestMapping("/api")
public class Example {
#PostMapping("/products/{product_id}/comments")
public ResponseEntity doSomething(#PathVariable("product_id") String id) {
// String matchedPath = getPath();
return ResponseEntity.ok().build();
}
}
I found it is doable via one of HandlerMapping attributes BEST_MATCHING_PATTERN_ATTRIBUTE
import javax.servlet.http.HttpServletRequest;
#RestController
#RequestMapping("/api")
public class Example {
#PostMapping("/products/{product_id}/comments")
public ResponseEntity doSomething(HttpServletRequest req, #PathVariable("product_id") String id) {
String matchedPath = String.valueOf(req.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
return ResponseEntity.ok().build();
}
}
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerMapping.html

Spring Boot controller does not redirect

I create the following controller:
#Controller
public class RootController {
#RequestMapping(path = "/login", method = RequestMethod.GET)
public String showLoginPage() {
return "redirect:resources/templates/login.html";
}
}
But, trying to map this endpoint, I receive 404-ERROR.
This is a target-page location:
What am I doing wrong?
#Controller
public class RootController {
#GetMapping("/login")
public String showLoginPage() {
return "login";
}
It works to me.I hope hepls you!
#Controller
public class RootController {
#GetMapping("/login")
public String showLoginPage() {
return "login.html";
}
This will work

How to ignore level #RequestMapping() and direct call method level #RequestMapping()?

How to do I ignore the class level #RequestMapping("/home") and directly call the method level #RequestMapping("/users") in Spring?
#Controller
#RequestMapping("/home")
#RequestMapping("/method1")
public void method1(){
...
}
#RequestMapping("/users")
public void listUsers(){
...
}
I want to call http://localhost:8080/users to invoke listUsers() method.
You cannot bypass requestmapping defined at class level. for If so why you want a class level mapping then... you can instead do something like this in the method level request mapping
#Controller
#RequestMapping("/home/method1")
public void method1(){
...
}
#RequestMapping("/users")
public void listUsers(){
...
}
In that case you may try this...
#Controller
#RequestMapping({ "/home", "/users" })
#RequestMapping("/method1")
public void method1(){
...
}
#RequestMapping(method="RequestMethod.GET")
public void listUsers(){
...
}
Change #RequestMapping("/users") for #RequestMapping(method=RequestMethod.GET)
From my understanding, what you expect is a class-level RequestMapping. The method-level RequestMapping should be under the path of class-level's.
I'll give you some examples:
#RestController
#RequestMapping("/home")
public class HomeController {
// Full path of following endpoint: /home/parents.
#RequestMapping(value = "/parents", method = RequestMethod.GET)
public ResponseEntity<List<People>> getParents() {
return ResponseEntity.ok(methodToGetParents());
}
For the path of "users" you should do it in another class (controller):
#RestController
#RequestMapping("/users")
public class UsersController {
// Full path of following endpoint: /users.
#RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<People>> getUsers() {
return ResponseEntity.ok(methodToGetUsers());
}

defining default url for controller spring mvc3

I am new to spring mvc3 development and was facing a minor issue (which I was didn't face with ASP.Net MVC3). I want to know the process of defining a default (or landing) URL for a controller.
I have an accounts controller where I do all account management related stuff. So all my urls are mapped to this controller. I want to know that how can I map my "/accounts" url request to hit openAccountsDashboard method?
Code -
.... imports...
#Controller
#RequestMapping(value = "/accounts/*")
public class AccountController {
#RequestMapping( value = "/", method = RequestMethod.GET)
public ModelAndView openAccountsDashboard(HttpServletRequest request) {
.....
return new ModelAndView("accounts/landing");
}
#RequestMapping( value = "/change-password", method = RequestMethod.GET)
public ModelAndView openPasswordChangePage(HttpServletRequest request) {
.....
return new ModelAndView("accounts/passwordChange");
}
... other actions...
}
Any help would be great!
Thanks
Try something like this:
.... imports...
#Controller
#RequestMapping(value = "/accounts/")
public class AccountController {
#RequestMapping( value = "", method = RequestMethod.GET)
public ModelAndView openAccountsDashboard(HttpServletRequest request) {
.....
return new ModelAndView("accounts/landing");
}
#RequestMapping( value = "/change-password", method = RequestMethod.GET)
public ModelAndView openPasswordChangePage(HttpServletRequest request) {
.....
return new ModelAndView("accounts/passwordChange");
}
... other actions...
}
Then you can use url like this:
http://localhost:8080/yourwebapp/accounts/
to hit openAccountsDashboard method.
Regards,
Arek

Categories

Resources