I am using Spring boot 2.1.1 and added following dependencies in gradle file
dependencies {
implementation(
'org.springframework.boot:spring-boot-starter-web',
'javax.servlet:jstl',
'org.apache.tomcat.embed:tomcat-embed-jasper',
)
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
application.properties
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
Controller class
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloJsp {
#RequestMapping(value="/welcome.html",method = RequestMethod.GET)
public ModelAndView Hello(){
return new ModelAndView("welcome");
}
}
Also I put it jsp file under /webapp/WEB-INF/jsp/
and I am getting
"Whitelabel Error Page" 404 not found error
Am i missing something ?
Related
Hi I m beginner and I have a simple problem, my url doesn't work. localhost:8080 work but not with /api. The project is built properly.
Localhost:8080
I put #ComponentScan("com.tutojwt.test.repository") because without it doesn't work.
Here my code
Controller :
package com.tutojwt.test.api;
import com.tutojwt.test.model.User;
import com.tutojwt.test.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
#RequestMapping("/api")
#RequiredArgsConstructor
public class UserController {
private final UserService userService;
#GetMapping("/users")
public ResponseEntity<List<User>>getUsers(){
return ResponseEntity.ok().body(userService.getUsers());
}
Testapplication :
package com.tutojwt.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan("com.tutojwt.test.repository")
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Project structure
MAJ : Some of you say that is ComponentScan the prob but there are error if I dont put com.tutojwt.test.repository or just com.tutojwt.test
#ComponentScan("com.tutojwt.test") error
Without ComponentScan
You should call http://localhost:8080/api/users . #ComponentScan shouldn't be necessary and might actually cause problems (I don't think your controller class is scanned now, and some default hateoas endpoint is the one you're calling). #SpringBootApplication annotation has #EnableAutoConfiguration annotation by default and should scan all component annotated classes. If not, check your class structure again.
i have looked at 5 tutorials; more than 10 stackoverflow or similar answers but i still haven't resolved that (apparently common) problem.
All i want to achieve is to set a custom JSON upon when exception are thrown on my API. But the Controller advice is never even instantiated (watch breakpoint never crossed.)
Here are the relevant files:
Main class:
package com.bancarelvalentin.plaxdmin
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication(scanBasePackages = ["com.bancarelvalentin.*"])
class Main
fun main(args: Array<String>) {
runApplication<Main>(*args)
}
Sample controller:
package com.bancarelvalentin.plaxdmin.controller
import com.bancarelvalentin.plaxdmin.playground.CustomException
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
#RestController
#RequestMapping("/")
class DummyCtrl : PlController() {
#GetMapping
fun throwError(): ResponseEntity<Any> {
throw CustomException()
}
}
Error handler:
package com.bancarelvalentin.plaxdmin.playground;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice;RestControllerAdvice
import org.springframework.web.servlet.configjava.annotationlang.EnableWebMvc;Exception
#RestControllerAdvice
public class GlobalExceptionHandler {
#ExceptionHandler
public Exceptionfun handleException(Exception ce: Exception): Exception {
return ce;
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/plaxdmin
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
Here is what i tried:
Adding other anotation to the main and/or the ErrorHandler class (#EnableWebMvc, #Component, some others i don't remember)
Putting the 3 above files in the same package
turning in and off the white label page
add a scanBasePackages attributes in my main class annotaation
newbie pb on Spring Boot, I cannot get a RestController to be added to my application.
Here are the 2 files used to build this very simple app with maven:
1) Application.java
package com.learn.hello;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan(basePackages = "com.learn.hello.controller")
#RestController
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
HelloController.java
package com.learn.hello.controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class HelloController {
#RequestMapping("/titi")
public String index() {
return "Greetings from titi Boot!";
}
}
Structure of the project
/src/main/java/com/learn/hello/Application.java
/src/main/java/com/learn/hello/controller/HelloController.java
/target/*
/pom.xml
localhost:8080 works
but localhost:8080/titi does not (404 not found)
Any idea ?
Thx
You forgot to provide request method RequestMethod.GET :
#RequestMapping(value = "/students", method = RequestMethod.GET)
You can use #GetMapping("/titi") instead of #RequestMapping("/titi") :
#GetMapping("/titi")
To all, thanks a lot to all for your answers.
#Valerio,
you were right it was correct!!! In fact an invisible char got added at the end of the filename: HelloController.java (me probably messing-up with Sublime...), thus it was not processed as a java file by maven...
Sorry for the noise.My apologies.
BTW if anybody knows about any sort of options to actually warn that a file located in the /src/main/java/* directories was not processed as a "regular java project" file I'm interested to know about it even though I doubt that this is really feasible and thus existing...
When I click on submit button I got this error message
Whitelabel Error Page This application has no explicit mapping for
/error, so you are seeing this as a fallback. Tue Jun 30 17:24:02 CST
2015 There was an unexpected error (type=Not Found, status=404). No
message available
Here is my code.
package com.tourpackage.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.tourpackage.model.TourPackage;
import com.tourpackage.repository.TourPackageMongoRepository;
import com.tourpackage.repository.VehicleTypeMongoRepository;
#Controller
public class TourPackageController {
#Autowired
TourPackageMongoRepository packageRepository;
VehicleTypeMongoRepository vehicleTypeRepository;
#RequestMapping("/tourpackage")
public String tourpackage(Model model){
model.addAttribute("packagelist", packageRepository.findAll());
return "index";
}
#RequestMapping("/addNewTour")
public String addNewTour(Model model){
model.addAttribute("packagelist", packageRepository.findAll());
return "tourpack";
}
#RequestMapping(value="/addPackage", method = RequestMethod.POST)
public String addPackage(#ModelAttribute TourPackage tourpack) {
packageRepository.save(tourpack);
return "redirect:tourpackage";
}
}
Spring Boot automatically registers the Basic ErrorController as a Spring Bean when you haven't specified an implementation for ErrorController.
so,
If you want to return customised content for path /error, refer following code:
#RestController
public class MyController implements ErrorController{
private static final String PATH = "/error";
#RequestMapping(value = PATH)
public String error() {
return "Error handling";
}
#Override
public String getErrorPath() {
return PATH;
}
}
Else,
If you want to disable it, you can refer this post:
http://www.logicbig.com/tutorials/spring-framework/spring-boot/disable-default-error-page/
I have a simple Spring back-end. It has a folder that contains controllers.
package com.movieseat.controllers;
// Java imports
import java.util.List;
// Spring imports
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
// Project imports
import com.movieseat.models.Movie;
import com.movieseat.services.MovieService;
#RestController
#RequestMapping("/api/movies")
public class MovieController {
#Autowired
private MovieService movieService;
#RequestMapping(value = "/allMovies", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Movie> getAllMovies() {
return movieService.getAllmovies();
}
}
In my Angular service I have a getAll() method:
public getAll<T>(): Observable<T[]> {
return this.http.get<T[]>('/api/movies/allMovies');
}
When I run the application I get a:
GET http://localhost:8090/api/movies/allMovies 404 ()
I have the server running on port 8090.
The following structure is used:
com
movieseat
Application.java
controllers
MovieController.java
models
MovieModel.java
repositories
MovieRepository.java
services
impl
MovieServiceImpl.java
MovieService.java
See if your controller class is picked up by spring scanning and performs mapping correctly. For example - If you are using Spring Boot, put a #SpringBootApplication on your main class that runs the app. The best way to know if your endpoint is scanned is to look for it when spring launches (in the log). You should look for something like
2017-09-17 14:45:49.522 INFO 2873 --- [main] RequestMappingHandlerMapping : Mapped "{[/allMovies],methods=[GET]}" onto public java.lang.String com.movieseat.controllers.MovieController.getAllMovies...