Spring Boot controller does not redirect - java

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

Related

How to get data from another service using Feign Client Spring Boot (ERROR: 406)

When I call another Service (User-service) from one service (API-gateway) Using Feign Client, I'm getting an Error
There are two services
User-service
API-gateway
In my API-gateway
FeignClient
#FeignClient(contextId = "user-by-email",name = "user-service")
#Service
public interface UserByEmail {
#RequestMapping(value = "/email/{email}", consumes= MediaType.APPLICATION_JSON_VALUE)
User findByEmail(#PathVariable("email") String email);
}
Controller
#RequestMapping("/test")
public class TestController {
#Autowired
private UserByEmail userByEmail;
#GetMapping(value = "/{email:.+}")
public ResponseEntity testUser(#PathVariable("email") String username) {
return ResponseEntity.ok(userByEmail.findByEmail(username));
}
}
I need to call the following (User-service)
Controller
#EnableFeignClients
#RestController
public class UserController extends BaseController<User> {
#Autowired
private UserService userService;
#PostConstruct
public void binder() {
init(this.userService);
}
#GetMapping(value = "/email/{email}")
public ResponseEntity findByEmail(#PathVariable("email") String email) {
return ResponseEntity.ok(userService.findByEmail(email));
}
}
Repository
#Override
public User findByEmail(String email) {
Query query = new Query(Criteria.where("email").is(email).and("status").is(1));
return mongoOperations.findOne(query, User.class);
}
Service
#Override
public User findByEmail(String email) {
return userDao.findByEmail(email);
}
The Error I'm getting is ..
<Map>
<timestamp>1583924335777</timestamp>
<status>406</status>
<error>Not Acceptable</error>
<message>Could not find acceptable representation</message>
<trace>org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:246)
Can anyone please explain What is wrong with my code, And give your valuable solutions
(Basically I need to create Security in API-gateway, in order to control the access of other services)
Try Implementing Feign client as below:
#FeignClient(name = "user-service")
public interface UserByEmail {
#RequestMapping(method = RequestMethod.GET, value = "/email/{email}", consumes = "application/json")
User findByEmail(#PathVariable("email") String email);
}
also make sure the Fields passed over JSON matchers User POJO.
I've got the solution to my Question
FeignClient
#FeignClient(contextId = "user-by-email",name = "user-service")
#Service
public interface UserByEmail {
#RequestMapping(value = "/email/{email}", consumes= MediaType.APPLICATION_JSON_VALUE)
User findByEmail(#RequestParam("email") String email);
}
Controller
#RequestMapping("/test")
public class TestController {
#Autowired
private UserByEmail userByEmail;
#GetMapping(value = "/{email}")
public ResponseEntity testUser(#RequestParam("email") String username) {
return ResponseEntity.ok(userByEmail.findByEmail(username));
}
}
In User-service
Controller
#EnableFeignClients
#RestController
public class UserController extends BaseController<User> {
#Autowired
private UserService userService;
#PostConstruct
public void binder() {
init(this.userService);
}
#GetMapping(value = "/email/{email}")
public ResponseEntity findByEmail(#RequestParam("email") String email) {
return ResponseEntity.ok(userService.findByEmail(email));
}
}

How to handle Angularjs routing in Spring MVC?

This is my Project structure:-
The following is my ngRoute code:-
mainApp.config(function($routeProvider) {
$routeProvider
.when('/', {
//Homepage
templateUrl: 'main.html',
controller: 'directoryController'
})
.when('/edit', {
//Edit Templates
templateUrl: 'details.html',
controller: 'tempUnguidedEditController'
})
});
This is my Spring controller class :-
#Controller
public class MainController {
#RequestMapping(value = "/home.html", method = RequestMethod.GET)
public ModelAndView home() {
return new ModelAndView("index");
}
#RequestMapping(value = "/dashboard.html")
public ModelAndView dashboard() {
return new ModelAndView("dashboard");
}
}
When I run the dashboard.html link it gives an error on console log:-
When I run the my web pages separately on browser it works but when I integrate it with Spring it fails to locate some files. I guess I am doing something wrong in the project structure which is why it is not able to find main.html file.
I got it to work by,
i) Converting main.html,guided.html and unguided.html to main.jsp,guided.jsp and unguided.jsp.
ii) Then i intercepted the requests for all of the three individual routes in my MainController class.
#Controller
public class MainController {
#RequestMapping(value = "/home.html", method = RequestMethod.GET)
public ModelAndView home() {
return new ModelAndView("index");
}
#RequestMapping(value = "/dashboard.html")
public ModelAndView dashboard() {
return new ModelAndView("dashboard");
}
#RequestMapping(value = "/main.html")
public ModelAndView main() {
return new ModelAndView("main");
}
#RequestMapping(value = "/guided.html")
public ModelAndView guided() {
return new ModelAndView("guided");
}
#RequestMapping(value = "/unguided.html")
public ModelAndView unguided() {
return new ModelAndView("unguided");
}
}

#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?

Second Controller can't find .html

First controller works correctly,
#Controller
#ComponentScan
#EnableAutoConfiguration
public class MainController {
#RequestMapping("/profile")
private String g(){
return "Profile.html";
}
}
Second:
#Controller
#RequestMapping(value = "/gallery")
public class GContoller {
#RequestMapping(value = "/month")
String ffff() {
return "monthGallery.html";
}
#RequestMapping("/test")
#ResponseBody
String test() {
return "Test";
}
}
Page localhost:8080/gallery/test opens and display "test", but localhost:8080/gallery/month opens with 404 error. Obviosly, that 2nd controller can't find file monthGallery.html. All htmls are in /webapp.
Project structure
/java
/com.example.xxx
/webapp

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