404 Not Found after deploy java web application .war file - java

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

Related

Get NOT_FOUND with Spring Boot?

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.

HTTP Status 404 – Not Found while deploying on remote Tomcat Server

I have a spring boot application with the main class like this
#SpringBootApplication
#EnableEmailTools
#EnableScheduling
#EnableJpaAuditing
public class SimApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SimApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SimApplication.class, args);
}
#Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
#Bean
public JsonViewSupportFactoryBean views() {
return new JsonViewSupportFactoryBean();
}
}
I also have controller for / like this
#Controller
public class WebController {
#GetMapping("/")
public String login(){
return "login";
}
}
Running locally it displays the login page. But when I generate war file and deploy it on Tomcat remote server, it always gives me this error
HTTP Status 404 – Not Found
Type Status Report
Message /
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.19
Url for localhost is http://localhost:8080/ this redirects me to Login. Now after deploying to Tomcat on a remote server, whenever I hit http://remoteurl:8080/artifact-id/ it sends me to the error page.
What am I missing? Any help would be appreciated. Thanks

No mapping found on Spring-Boot

I'm new a spring-boot and spring framework. According to me, web app create and deploy very easy with spring-boot but when i run my sample spring web app, application not found "welcome.html" page. I checked all similar question on stackoverflow and not worked me. I cannot see little issue but I didnt find my problem. My application structure and codes are below:
MyApplication class is below:
#SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
WelcomeController class is below:
#Controller
public class WelcomeController {
#RequestMapping(value = "/welcome")
public String welcome() {
return "welcome"; //<- this is your login.html if it is directly in resource/templates folder
}
}
application.properties file is below:
spring.mvc.view.prefix = templates/
spring.mvc.view.suffix = .html
spring.mvc.static-path-pattern=/resources/**
WebMvcAppConfig class is below:
public class WebMvcAppConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry); //To change body of generated methods, choose Tools | Templates.
registry.addViewController("/welcome").setViewName("welcome.html");
}
}
Firstly thanks a lot for quickly response my question #Andy Wilkinson and georges van. I looked for in spring boot reference guide and Serving Web Content with Spring MVC and I learned a lot of information about spring-boot. I removed WebMvcAppConfig because this class not necessary for starter and removed SpringBootServletInitializer. I moved html files into templates as you say. I keep simple and application run without issues.

Spring Boot POST request doesn't work

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();
}

What should the template path be when using Spring Boot and a Sitemesh filter?

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'

Categories