0


I am trying to create a simple REST project with Maven in Eclipse.

I have created a new Dynamic Webapp Project and than right click --> configure/ convert Maven Project.

Now If I create a simple index.html with only one h1, it works.

So I created the GreetingController.java:

package resources;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

and the model: Greeting.java

package resources;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

and the 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>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>

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

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

    <properties>
        <java.version>1.8</java.version>
    </properties>


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

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
    <packaging>war</packaging>
</project>

If I go to http://localhost:7001/demo/greeting , it doesn't work (404). Same problem with http://localhost:7001/greeting .
I think the problem is that the server doesn't map the annotation.

Lorenzo Tassone
  • 373
  • 1
  • 4
  • 14

1 Answers1

0

The RequestMapping value should not include the context root. Change the value to

 @RequestMapping("/greeting")
Brian Ochs
  • 1,099
  • 1
  • 10
  • 21
  • Yeah, that was the original version. @RequestMapping("/demo/greeting") was a test. In both case doesn't work. – Lorenzo Tassone Nov 02 '15 at 15:42
  • Does it work if you run the application as a standalone Spring Boot application? – Brian Ochs Nov 02 '15 at 15:48
  • I've followed the tutorial: spring.io/guides/gs/rest-service and I've also created the main class Application.java but the result is the same (404). – Lorenzo Tassone Nov 02 '15 at 16:06
  • EDIT: I tried it by right clicking on Application.java Run As java application. On port 8080 (Tomcat) it works. So I think the problem is that Weblogic build the project not considering Maven dependences. – Lorenzo Tassone Nov 02 '15 at 16:24
  • What version of WebLogic are you using? Are you running it with JDK 8? The 404 could mean that the application isn't installing at all in WebLogic, but you would see that in the logs. – Brian Ochs Nov 02 '15 at 16:36
  • I have a simple index.html and it works. Weblogic 12.2.1 and JDK 8 – Lorenzo Tassone Nov 02 '15 at 16:42
  • This question might help. http://stackoverflow.com/questions/24835051/deploy-spring-boot-app-in-weblogic – Brian Ochs Nov 02 '15 at 17:04