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

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

Related

How to find webservice path in heroku?

So I have a web app with a specific route: http://localhost:28389/MyProjectName/webresources/test/ this returns just a json string:
But when I deploy my app on heroku the web app can't find the same path:
This is my ApplicationConfig:
#javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(pkgServices.TestService.class);
}
}
And this is my TestService class:
#Path("test")
public class TestService {
public TestService () {
}
#GET
#Produces({MediaType.APPLICATION_JSON})
public Response getTestMsg() throws Exception {
return Response.ok("welcome").build();
}
}
Read Heroku docs. They allocate port number for you and set it to environment variable. You have to use this environment variable in your application to bind http server to this port.
Note, you will still connect to your app at 80th port from browser.
Current problem is Heroku cannot forward request to your server because you do not listen on port which Heroku expects you to listen to.

404 Not Found after deploy java web application .war file

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

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.

How to map the hibernate to the JSP view?

Following is a folder hierarchy in the Spring boot Hibernate project ,
This is a method in UserResource Controller
#GetMapping("/")
public ModelAndView home(HttpServletRequest request){
//request.setAttribute("mode", "MODE_HOME");
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
Following code also added to the application.properties file to find the correct jsp page.
spring.mvc.view.prefix:WebApp/Web/
spring.mvc.view.suffix:.jsp
But this is not worked and following error is occurred.
Anyway, I tried many ways by modifying the path in the 'application.properties' file and couldn't find the right solution. Are there any steps to fire the jsp view?
If your main class don't extends SpringBootServletInitializer ,try this.
public class SpringHibernateAssignmentApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringHibernateAssignmentApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringHibernateAssignmentApplication.class, args);
}
}

Spring boot addInterceptors not being called in Websphere application server

I have a Spring Boot app using SpringWS. Inside of the WsConfigurerAdapter I am overriding addInterceptors in order to add logging/authentication/validation/etc.
#Configuration
#EnableCaching
#EnableWs
public class WebServiceConfig extends WsConfigurerAdapter {
...
#Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
EnvironmentSettings environment = getEnvironmentSettings();
interceptors.add(getLogSetupInterceptor());
interceptors.add(getAuthenticationInterceptor());
interceptors.add(getServerLoggingInterceptor());
interceptors.add(getAuthorizationInterceptor());
ServerPayloadValidatingInterceptor validatingInterceptor = new ServerPayloadValidatingInterceptor();
validatingInterceptor.setValidateRequest(environment.isValidateSoapRequest());
validatingInterceptor.setValidateResponse(environment.isValidateSoapResponse());
validatingInterceptor.setXsdSchema( xsdSchema());
interceptors.add(validatingInterceptor);
}
}
What is strange is that when run locally, all of these interceptors are being added and run for every request just fine. However, when I deploy the application as a .war to WAS, this one method is not being run. I even added logging statements and I can tell that it's this method that's getting skipped over instead of the interceptors themselves. Does anybody know something about spring boot .war files that I don't?
Also, here is my Application class:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I figured out what I did wrong. It turns out I was creating a servlet mapping in both my WebServiceConfig and my web.xml both at /*.

Categories