I'm new to Spring framework.
I try to make a simple web server application with Spring but I got 404 Not Found when I call url with Postman.
package com.leoaslan.doctorfinder;
//..import
#SpringBootApplication
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
#ComponentScan({"com.delivery.request"})
#EntityScan("com.delivery.domain")
#EnableJpaRepositories("com.delivery.repository")
public class DoctorfinderApplication {
public static void main(String[] args) {
SpringApplication.run(DoctorfinderApplication.class, args);
}
}
package com.leoaslan.doctorfinder.controller;
//import
#RestController
#RequestMapping("/api")
public class LoginController {
private final Logger log = LoggerFactory.getLogger(LoginController.class);
#Autowired
LoginService loginService;
#GetMapping("/auth/login")
ResponseEntity<?> login(HttpServletRequest request) {
System.out.println("OK");
return new ResponseEntity<>(HttpStatus.ACCEPTED);
}
}
I haven't configured anything yet in application.properties.
Thanks for any helps
Your Controller class is not being scanned, so just try to add the proper package of your controllers on your #ComponentScan
#ComponentScan({"com.delivery.request", "com.leoaslan.doctorfinder.controller"})
Actually, do you really have those packages (com.delivery.request, com.delivery.domain, com.delivery.repository) on your application ? They look suspiciously copy/pasted and they will not do anything unless you change them where your classes really are.
Related
I'm wrote a web application in java using Spring framework. Tested it and deployed to remote tomcat server. After deploying I have message OK - Started application at context path [/proxynator]. But, if I use links like http://109.206.178.66:8080/proxynator/ and http://109.206.178.66:8080/proxynator/proxynator/test I have 404 – Not Found and Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
In Application I have starter class
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
and controller
#RestController
#RequestMapping("/proxynator")
public class MainController {
#Autowired
private ProxyRepo proxyRepo;
#Autowired
private CountryRepo countryRepo;
#RequestMapping("/countries")
#ResponseBody
public List<Country> findCountries() {
return countryRepo.findAll();
}
#RequestMapping("/test")
#ResponseBody
public String testMethod() {
return "HELLO";
}
}
I don't know, why I have this problem, because I setting up my tomcat server right, path to my controller is right and application on server is running.
Any ideas how to solve it?
UPD
I was changed my controller like:
#RestController
public class MainController {
#Autowired
private CountryRepo countryRepo;
#RequestMapping("/countries")
#ResponseBody
public List<Country> findCountries() {
return countryRepo.findAll();
}
#RequestMapping("/")
#ResponseBody
public String testMethod() {
return "HELLO";
}
}
And now my enter point is / that calling testMethod(), but it doesn't working too.
To solve this problem I was extends SpringBootServletInitializer and override configure method`
#Override
override fun configure(application: SpringApplicationBuilder?):
SpringApplicationBuilder {
return super.configure(application)
}
and I changed project structure like in official documentation. And now it works good.
PS
I was rewrite project to Kotlin
I'm building a spring boot application. My problem is that when I run the project, my login page is not shown. This is my code:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class RestLogin {
#RequestMapping("/")
public String login() {
return "login";
}
}
I get only a white page and "login" is written in it. When I try to change the #RestController with #Controller I get this GET http://localhost:8080/ 404 (). My login.html page is located under the webapp>tpl>login.html
How can I display my login page?
Edit
This is my application class
#SpringBootApplication
public class ExampleApplication extends SpringBootServletInitializer {
private static Logger logger = LoggerFactory.getLogger(ExampleApplication.class);
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ExampleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
I dont know your configuration but:
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests()
.antMatchers("/**").permitAll();
http.authorizeRequests().antMatchers("/**").permitAll();
}
}
In the Application.properties file add:
spring.mvc.view.suffix: .html
Change #RestController to #Controller for RestLogin class. Also put your html file inside the static folder inside resources folder.
You need an application class with a main method. See this tutorial.
Here's a snippet:
package 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;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
This is the normal behavior.
New version of Spring web comes with #RestController annotation which nothing but #Controller + #ResponseBody. So when you have a return statement in a method you must use #RestController or annotate your method with #ResponseBody.
Here the problem is that Spring don't know a lot about the http method type, can you please try to use #GetMapping("/") to combinbe path and method at the same time.
According to your posted code and your description, you're getting an expected behavior.
When you annotate your controller with #RestController, that means that your methods on that controller will try to return their result as JSON.
According to your code:
#RestController
public class RestLogin {
#RequestMapping("/")
public String login() {
return "login";
}
}
You're returning the String "login", that's why you're getting empty white page with the word login as JSON
If you will change the #RestController to #Controller then it no longer will return your string as JSON,
but Spring will try to figure out from the that "login" string a view, and for that you'll need to add a view resolver bean to your project.
I am trying to build Spring boot REST application with angularJS.
So, application is loading well, every JS and CSS files included. The problem is that when I am doing GET request, it goes right way, but when I am doing POST request it fails and doesn't try to call the controller method.
That's my Spring Boot Application class
#EnableAspectJAutoProxy(proxyTargetClass=true)
#SpringBootApplication(scanBasePackages = {"org.test.controllers", "org.test.services"})
#Import({ WebSecurityConfig.class, DBConfig.class, ViewConfig.class})
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
System.out.println("STARTING APP");
SpringApplication.run(Application.class, args);
}
}
And that's my controller class
#RestController
#RequestMapping("/tag")
public class TagController {
#Autowired
private TagService tagService;
#RequestMapping(method = RequestMethod.GET)
#ResponseStatus(HttpStatus.OK)
public Iterable<Tag> getAllTags() {
return tagService.getAll();
}
#RequestMapping(method = RequestMethod.POST)
#ResponseStatus(HttpStatus.CREATED)
public Tag saveTag(#RequestBody Tag tag) {
return tagService.save(tag);
}
}
So, when I am doing $http.get("/tag", success, error) it gives [], which means that controller was called.
And when I am doing $http.post("/tag", {name: 'name'}, success, error) it returns {"timestamp":1489939480282,"status":404,"error":"Not Found","message":"No message available","path":"/tag"}
To make sure that mapping was done, here's part of logs
Mapped "{[/tag],methods=[POST]}" onto public org.test.model.Tag org.expotest.controllers.TagController.saveTag(org.test.model.Tag)
I am running on Tomcat server if it matters.
Any ideas what could be wrong in my configuration? That seems really strange for me.
Thanks in advance.
If you are using Spring security try disabling csrf in your configure method, should look something like this:
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
}
I'm trying to create à Rest controller that listen on "/login" I have defined the code bellow but when I open http://localhost:8080/login I get a 404 error...
Please help :)
Here is my package structure:
com.my.package
|_ Application.java
|_ controller
|_ LoginController
My Application:
#ComponentScan("com.my.package.controller")
#SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
My Rest controller:
#RestController
public class LoginController {
#RequestMapping("/login")
public #ResponseBody String getLogin(){
return "{}";
}
}
The controller class should be in a folder of the Application class or in the lower folder.
So, if the application class is in package com.company.app, then the controller class should be in package com.company.app or in com.company.app.*. Let say the controller class is in com.company.controller, it will not mapped since its not in same package or child package of application class.
You should use this annotations in your init class of your springBoot App
#Configuration
#EnableAutoConfiguration
#ComponentScan("com.my.package")
public class WebAppInitializer{
public static void main(String[] args) throws Exception{
SpringApplication.run(WebAppInitializer.class, args);
}
}
Sometimes, when it makes no sense why the mapping is not working, just change the server port so something different than 8080.
ie. in the application.properties or yml, server.port=8081
I'm trying to make use of Sitemesh (3) templating with Spring Boot (4+) using Java annotation based config.
When I hit the controller URL, the handler method is invoked.
The Sitemesh filter is activated (debugging proves that).
However I am getting a 404, which I believe is because with the config I have the Freemarker template isn't found (wrong path somewhere).
Code follows, any suggestions what I'm doing wrong would be great!
Filter:
#WebFilter
public class SitemeshFilter extends ConfigurableSiteMeshFilter {
#Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
System.out.println("in sitemesh filter");
builder.addDecoratorPath("/*", "templates/main.ftl")
.setMimeTypes("text/html")
.addExcludedPath("/javadoc/*")
.addExcludedPath("/brochures/*");
}
Controller:
#Controller
public class UserController {
#Autowired
MemberService memberService;
#RequestMapping(value="member/{id}")
public ModelAndView viewMember(#PathVariable("id") int memberId, ModelAndView mv) {
mv.setViewName("member");
ClubMember member = memberService.getClubMember(memberId);
mv.addObject("member", member);
return mv;
}
}
Main class:
#SpringBootApplication
#ServletComponentScan
#EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class ClubManagementApplication {
public static void main(String[] args) {
SpringApplication.run(ClubManagementApplication.class, args);
}
}
Application.properties:
spring.mvc.view.prefix=/views/
My templates live in :
src/main/resources/templates <- this is where I've put the sitemesh templates live
src/main/resources/views <- here are the Freemarker pages
In case anyone else has same issue:
templates ended up in resources/templates
sitemeshfilter:
#Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addDecoratorPath("/*", "/main.ftl")
.setMimeTypes("text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml");
}
Note nothing before '/main.ftl'