Spring MVC RequestMapping not working on RestController - java

I want to have a RestController-class with the base-mapping "/user" (so the different functions will have paths like "/user/add", "/user/remove" etc or use POST/GET etc)
This is the part that I don't understand and can't get to work:
#RestController
public class UserController {
#GetMapping("/user")
public Response login(Principal principal){
//some output
}
}
Expected behavior for this case would be that I can access my output under "/user". This works as expected.
Now if I modify it to the following (since all functions in this controller should have a path starting with "/user" this would be cleaner)
#RestController
#RequestMapping("/user")
public class UserController {
#GetMapping("/")
public Response login(Principal principal){
//some output
}
}
I get a 404-Error page and can't access "/user" anymore
All examples I have found use the same syntax (or sometimes #RequestMapping(path="/user") but that didn't work as well) and I don't know why it doesn't work.
Can someone tell me where my mistake is?

If you use this code:
#RestController
#RequestMapping("/user")
public class UserController {
#GetMapping("/")
public Response login(Principal principal){
//some output
}
}
Then your url should have "/" at the end like "http://localhost:8080/user/"
I would just throw away "/" symbol from #GetMapping("/") and left like this:
#RestController
#RequestMapping("/user")
public class UserController {
#GetMapping
public Response login(Principal principal){
//some output
}
}
And if you need map get or post you can use like this:
#RestController
#RequestMapping("/user")
public class UserController {
#GetMapping("/add")
public SampleObj getJson() {
return new SampleObj();
}
}
It should work.

Related

how can I do to make it return error when restful url has invalid parameter

#RestController()
#RequestMapping(path = "/users")
public class UserController {
#GetMapping()
public #ResponseBody Page<User> getAllUsers(#RequestParam Integer pageSize, UserRequest userRequest) {
//TODO: some implementation
}}
public class UserRequest{
public String name;
public String age;
}
send the request with invalid parameter, like localhost:8800/users?name1=1234, I want to return error. but in fact it ignore the invalid parameter name1.
I tried to add the user defined annotation on the method parameter and on the class , codes like below
#RestController()
#RequestMapping(path = "/users")
#Validated
public class UserController {
#GetMapping()
public #ResponseBody Page<User> getAllUsers(#RequestParam #Validated Integer pageSize, #Validated UserRequest userRequest} {
//TODO: some implementation
}
}
But it does not working.
I think it is happened because framework has ignore the invalid parameter before the method was called.
where did framework handle the url and how can I do to make it return error instead of ignore?
You can reject parameters that are not valid. You can do so in a HandlerInterceptor class.
Reference: Rejecting GET requests with additional query params
In addition to what is done in the above reference, in your addInterceptors, you can specify the path that is intercepted.
Like this:
#Configuration
public class WebConfig implements WebMvcConfigurer {
private String USERS_PATH = "/users";
// If you want to cover all endpoints in your controller
// private String USERS_PATH = List.of("/users", "/users/**");
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new FooHandlerInterceptor()).addPathPatterns(USERS_PATH);
}
}

SpringBoot with RequestMapping on Application and another RequestMapping in controller classes, is that possible?

I am learing SpringBoot and Rest api just now and in all examples I have seen, in every controller classes they have a #RequestMapping("/api/v1") and then some #GetMapping or simular.
But it feels like why am I writing a RequestMapping for every controller if the first URI mapping is the same for all controllers.
Is it possible to have a #RequestMapping in the SpringBootApplication that maps "/api/v1" and then in the controller classes then have another ®RequestMapping for subfolders like "/products" and simular.
I have tried to solve this, but I cant make it function.
#SpringBootApplication
#RestController
#RequestMapping("/api/v1")
public class CommonApplication {
public static void main(String[] args) {
SpringApplication.run(CommonApplication.class, args);
}
}
#RestController
#RequestMapping()
public class ProductController {
private ProductService productService;
#GetMapping("/products")
public String getProducts()
{
return "Hello from getProducts 12";
}
}
I want this full URI "/api/v1/products" to function and return the String text. But I only get 404.
It is possible to have request mapping in both application and controller class. Go through the documentation which is attached below once for having better understanding.
https://zetcode.com/spring/requestmapping/

#Path works, but #RequestMapping doesn't Java Spring Boot

This works, I am able to make a request in postman for this Service.
#RestController
#Path("sample")
public class SampleClass {
#GET
#Path(value = "/s1")
public Object get() {
//Something
}
}
The problem is when I try to use #RequestMapping instead of #Path, I get a
404 Not Found
Error.
#RestController
#RequestMapping("sample")
public class CommonService {
#GetMapping(value = "/s1", produces = MediaType.APPLICATION_JSON)
public Object get() {
//Something
}
}
What I am doing wrong here?
After a while, I found out that for the JAX-RS (#Path) I had configured in web.xml file a different route "something".
JAX-RS: localhost:8080**/something**/sample/s1
Spring Rest Services: localhost:8080/sample/s1
I was also missing a "/" in the Spring Rest Service.
#RequestMapping("**/**sample")
Full code bellow:
#RestController
#RequestMapping("/sample")
public class CommonService {
#GetMapping(value = "/s1", produces = MediaType.APPLICATION_JSON)
public Object get() {
//Something
}
}

#RestController does not work, it still return me a jsp page

Controller:
#RestController
public class CommodityController {
#RequestMapping("test.htm")
public String test() {
return "test";
}
}
Spring version:
<org.springframework.version>4.2.0.RELEASE</org.springframework.version>
Still not working:
Can I use #RestController like this?

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

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

Categories

Resources