1

Hi I am using Spring boot with maven jar packaging. My project structure looks like this enter image description here

I am starting my project from command promt with this command: mvn package && java -jar target/ProjekatNekretnine-0.0.1.jar, and it starts perfect. Than I go to http://127.0.0.1:8080/api/users/home and I do not get my welcome.jsp page. My controller class looks like this:

UserController class:

package com.projekat.nekretnine.controller;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.projekat.nekretnine.Model.Korisnik;

@RestController
@RequestMapping(value="api/users")
public class UserController {

    @RequestMapping(value = "/home")
    public String welcome(Model model) {
        return "welcome";
    }

}

I think that my welcome method should return my welcome.jsp file, but instead it writes "welcome" to the page. Why is that so? What am I doing wrong?

My welcome.jsp file looks like this:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>This is working.</h1>
</body>
</html>

So you see that it shouldn't write welcome.

Here is my pom.xml for you to take a look:

<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>com.projekat.nekretnine</groupId>
  <artifactId>ProjekatNekretnine</artifactId>
  <version>0.0.1</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

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

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

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

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

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

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!-- <version>5.1.30</version> -->
        </dependency>

    </dependencies>

    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

One more thing, when I created maven project, I selected that the packaging should be JAR, not WAR. I don't know if I need now web.xml or other xml files, I think not. Maybe the mistake is where I put my static files, if that's the problem, can you tell me where to put tham?

And I tried to use @Controller, instead of @RestController in my UserController class, and still the same problem.

Thank you in advance.

P3P5
  • 923
  • 6
  • 15
  • 30
  • You're doing a Web Application, you have to make a WAR, not a JAR. WAR = Web Application Archive – Tunaki Nov 27 '16 at 20:44
  • Add the code for your SpringBootServletInitializer class and also add if there are any other config classes – Vasu Nov 27 '16 at 20:45

1 Answers1

2

The behaviour your are experiencing is normal. This happens because you instructed your controller( specifically your rest-controller ) to return a String object when it hits /api/users/home endpoint. If you however want to expose a page(view) in this endpoint you should configure your spring-boot to use a InternalResourceViewResolver.

You could take a look at a similar question Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error. and apply the changes for your controller to return the welcome page.

Community
  • 1
  • 1
Alex Ciocan
  • 2,272
  • 14
  • 20