I'm trying to run my web application based on javax.mvc, but I get 404 error. I think there should be an issue related to the application path, but I don't know exactly what's wrong.
XAMPP is installed and IntelliJ idea is configured to run tomcat7 as the web server.
Application code:
#ApplicationPath("web")
public class StoreApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<>();
set.add(ProductController.class);
return set;
}
}
Controller:
#Controller
#Path("products")
public class ProductController {
#Inject
private Models models;
#GET
public String list() {
models.put("products", Product.list());
System.out.println("helllo");
return "/WEB-INF/jsp/list.jsp";
}
}
This is a maven project. The build process is done with IntelliJ idea default settings.
Base url is set in StoreApplication.java as web with application path.
The war url is http://localhost:8080/elearning_war/ as depicted in the picture.
And the controller path is products. So I expect to show a list of products in http://localhost:8080/elearning_war/web/products, but instead, I get a 404 error page.
In my opinion, because of #ApplicationPath("web") the URL should be http://localhost:8080/web/products.
Related
I am trying to create a RESTful Service in Java using Eclipse, but I have hardly ever worked with this types of projects and I'm getting quite lost trying to create the path of my service, always getting 404 Not Found.
This is my Java class prueba.MyService.java:
package prueba;
import ....
....
#Path("/service")
public class MyService{
#Path("/hello")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello World()";
}
...
}
This is my prueba.ApplicationConfig.java class, in which I have added the #ApplicationPath annotation:
package prueba;
import ...
...
#ApplicationPath("/rest")
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(prueba.MyService.class);
return classes;
}
}
I have also tried to add the <servlet> and <servlet-mapping> tags in the web.xml file, but it didn't solve anything, and based on what I have read here, with the #ApplicationPath annotation should be enough.
After this, I run my service with an Apache Tomcat server and I try to access the http://localhost:8080/rest/service/hello URL, but I get the 404 Not Found error.
Sorry in advance if I am missing something really basic or stupid.
Found the solution by myself:
I have finally started all over again following this tutorial and now it works correctly. Hope it helps.
I've created an external JAR which contains a simple Spring REST controller, which contains the following piece of code:
#RestController
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(value = "/world")
public Hello hello() {
System.out.println("HELLO WORLD");
return new Hello(1L, "Hello World");
}
}
I then compile this small project into a jar called hello.jar which i then add to the class path of my Spring Boot application and start the application.
I've also added the package to ComponentScan like so:
#SpringBootApplication
#Configuration
// #Controller
// #org.springframework.context.annotation.Configuration
#ComponentScan(basePackages = {"main.java.foo.hello" })
#EnableEntityLinks
#EnableAutoConfiguration
public class Main {
public static void main(final String[] args) throws Exception {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)classLoader).getURLs();
for (URL url : urls) {
if (url.getPath().contains("hello")) {
System.out.println(url.getPath());
}
}
SpringApplication.run(Main.class);
}
}
Because of the print out I can see that the jar is loaded into the application and by adding logging to the Spring Boot Application I can see that the Controller is scanned and is picked up (or at least it seems to be).
However went I browse (via chrome) or make a REST call (via Advance REST client) to "localhost:8765/hello/world" (I start my server on port 8765), I get 404 error.
Other Rest Controllers (from within the application, not the external JAR) seem to be working fine as all REST calls return the appropriate results.
Does anyone think they know why 404 is returned?
I have a Spring (4) MVC servlet running on Tomcat 8 in Eclipse. When I start tomcat, there are no errors in the console and all the correct request mappings for my controllers are logged. If I try to access localhost:8080/app/login my controller method executes (checked via debugging), but I get a 404 page with the following:
message /app/WEB-INF/jsp/login.jsp
description The requested resource is not available.
My project has the following directory structure:
project-root
|-src
|-WebContent
|-WEB-INF
|-jsp
|-login.jsp
My configuration class:
#EnableWebMvc
#Configuration
#ComponentScan(basePackages = "com.example")
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/jsp/", ".jsp").viewClass(JstlView.class);
}
//Other stuff
}
Controller:
#Controller
#RequestMapping("/login")
public class AuthnRequestController {
#RequestMapping(value = "", method = RequestMethod.GET)
public ModelAndView getLoginPage() {
return new ModelAndView("login");
}
//Other stuff
}
The application was working fine in the past, but I was screwing around with my workspace/projects working on something else, and am unable to get this working again now that I'm coming back to it.
AFAIK, By default in a maven war project the jsp files are expected under /src/main/resources/. Since you have given a jsp file prefix of /WEB-INF/jsp/ in your config, please try moving the jsp files to the below location.
/src/main/webapp/WEB-INF/jsp/
Assumptions:
a mapping to root/WebContent is not provided in Web deployment assembly.
a mapping to /src/main/webapp is present in Web deployment assembly.
your eclipse is using maven war plugin
I am working on a web application using SpringBoot.
The problem I am facing is as follows. The application is running fine from Eclipse, but when I deploy the project as a war in tomcat 7, it's giving me "HTTP Status 404". No exception found in tomcat logs.
Below is my controller:
#RestController
public class TinyUrlController{
#Autowired TinyUrlService tinyUrlService;
#RequestMapping("/create")
public String createUrl(#RequestParam("url") String url,HttpServletRequest request){
TinyUrl tinyUrl = tinyUrlService.createUrl(url);
return tinyUrl.toString();
}
}
Seems your application have no entry point that's why you got nothing. Just create entry point into your application.
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<Application> applicationClass = Application.class;
}
See also Spring Boot deploying guide
I suggest that you try to build your application layout by using
http://start.spring.io/
It will make a SpringBoot application
It will make java packages right too
Just remember to place your controllers under java package "demo".. otherwise they cannot be "auto wired" without more configuration...
I suggest you make a simple controller that just returns "hello" as a starter...
I have a problem when i want to expose my EJB Project as JAX-RS service. I've tried to find a solution many times but i've not fixed it. I've succeeded deploy my application but i didn't found my rest service in localhost:4848 => Applications => My_Application. Normally, if a rest service is deployed, there is "Launch" button.
I use glassfish4 and eclipse Java EE.
My EJB Project is like that:
Package ejb: TestSessionBean.java
Package rest: RestTest.java and RestTestApp.java
TestSessionBean.java
#Path("/peter")
#Stateless
#LocalBean
public class TestSessionBean {
public TestSessionBean() {
// TODO Auto-generated constructor stub
}
#GET
public String sayHello() {
return "Hello";
}
}
RestTest.java
#Path("/ep")
public class RestTest {
#GET
public String sayBonjour() {
return "Bonjour";
}
}
RestTestApp.java
#ApplicationPath("/test/*")
public class RestTestApp extends Application {
}
I also tried to config my project: Properties => Project Facets => enable JAX-RS (but when i clicked on "Further configuration available", i had "Type: Disable Library Configuration".
I tried localhost:8080/ejbtest/test/peter/ and localhost:8080/ejbtest/test/ep but both didn't work.
But if i create a new Dynamic Web Project all and copy all of my source files in ejbtest into this project. It works! So i think about something to do in eclipse configuration of ejb project. Any solution?
Thank you in advanced.