0

i'm quite new to web developpement, I worked this summer on a web app on Spring, but the app was already set up, and was just doing some Java and AngularJS. But now i got some problem to start from scratch this type of application. I was trying to just test a simple web service, but can't reach the url I want, i get a 404 error. I'm working on Intellij with Tomcat 8.5.4 :

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <modelVersion>4.0.0</modelVersion>
    <groupId>seb</groupId>
    <artifactId>webapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.10.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xmlns:jpa="http://www.springframework.org/schema/data/jpa"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/context">

       <jpa:repositories base-package="repository" />
       <mvc:annotation-driven/>
       <context:component-scan base-package="controller" />

</beans:beans>

web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>springrest</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springrest</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Main.java

@SpringBootConfiguration
@EnableJpaRepositories("repository")
public class Main {

    public static void main (String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

StudentController.java

@RestController
public class StudentController {

    private final StudentService studentService;

    public StudentController(final StudentService studentService) {
        this.studentService = studentService;
    }

    @RequestMapping(value = "/student", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void getSomeStudent() {
        studentService.getSomeStudent();
        System.out.println("Hello");
    }
}

The url i'm trying to reach is : localhost:8080/webapp/student, but nothing happen, just have a 404 error. I guess i forgot something, but I looked at some tutorials and questions on the web, and can't figure how to fix my project. So if someone got some clues that can help me, that will be very helpful. Thanks by advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Do you want to use Spring Boot? Because your have `@SpringBootConfiguration` but a invalid config files. Otherwise if your project starts properly try to get `localhost:8080/student` without webapp – jahra Sep 07 '16 at 10:18
  • Instead of '@SpringBootConfiguration' add '@SpringBootApplication'. if you are using spring boot – sawan Sep 07 '16 at 10:21
  • If you want to create spring boot project just follow spring docs guid : https://spring.io/guides/gs/spring-boot/ – sawan Sep 07 '16 at 10:31
  • @jahra Yes i want to use springboot. What did i do wrong in the config files ? I also tried the url but that was not the probleme unfortunately. I also change the annotation on the Main class. – Sebastien MURE Sep 07 '16 at 10:31
  • First you should generate your project via http://start.spring.io/ It wiil generate project with needed dependencies and correct pom file. Spring boot doesn't need xml config files. And follow https://spring.io/guides/gs/spring-boot/ as @swan mentioned – jahra Sep 07 '16 at 10:32
  • Use https://start.spring.io/ to create you project and just add your webservices there – sawan Sep 07 '16 at 10:38
  • In dependencies add your JPA, Web... – sawan Sep 07 '16 at 10:39
  • @jahra and swan, Ok thanks, i'm going to take a look at these tutorial. – Sebastien MURE Sep 07 '16 at 10:39

3 Answers3

0

change @SpringBootConfiguration to @SpringBootApplication it configures following for you

  • @Configuration tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up
  • @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.

Since you are using spring-boot, add the following dependency,and remove web.xml where spring-boot automatically configures web dispatcher servlet for you

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

or you can still go ahead and configure one if you want.

Based on your initial settings, controller should be located at

http://localhost:8080/student (no 'webapp')

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
0

Well I stopped trying to deploy my app on tomcat, i'm just using spring now and i can reach my url. Anyway i will take deeper look at spring docs and tutorials, thanks for the different explications and link.

-1

Use https://start.spring.io/ to create your Spring Boot Application project.

This will generate spring boot maven project for you.

Then add your StudentController there.

and afer that as @jahra specified, you have to trigger URL : http://localhost:8080/student to access your student webservice

sawan
  • 2,341
  • 3
  • 25
  • 51