【问题标题】:Dynamic webapp project with Maven in Eclipse with weblogic server在 Eclipse 中使用 Maven 和 weblogic 服务器进行动态 webapp 项目
【发布时间】:2016-02-02 11:54:56
【问题描述】:


我正在尝试在 Eclipse 中使用 Maven 创建一个简单的 REST 项目。

我创建了一个新的动态 Webapp 项目,然后右键单击 --> 配置/转换 Maven 项目。

现在,如果我创建一个只有一个 h1 的简单 index.html,它就可以工作。

所以我创建了 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));
    }
}

和模型: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;
    }
}

和 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>

如果我去 http://localhost:7001/demo/greeting ,它不起作用 (404)。 http://localhost:7001/greeting 也有同样的问题。
我认为问题在于服务器没有映射注释。

【问题讨论】:

  • 错误信息是什么?日志中有什么?
  • 日志中没有任何内容。我有一个 404
  • 您的应用程序的上下文根是什么? RequestMapping 值中不需要它。
  • 在 weblogic.xml 中,上下文根是 demo。无论如何,我也试过localhost:7001/greeting,但我得到了同样的错误
  • 你在扫描你的控制器所在的包吗?你需要告诉 spring 控制器在哪里

标签: java spring maven model-view-controller weblogic


【解决方案1】:

RequestMapping 值不应包含上下文根。将值更改为

 @RequestMapping("/greeting")

【讨论】:

  • 是的,那是原始版本。 @RequestMapping("/demo/greeting") 是一个测试。在这两种情况下都不起作用。
  • 如果将应用程序作为独立的 Spring Boot 应用程序运行,它是否工作?
  • 我遵循了教程:spring.io/guides/gs/rest-service,并且我还创建了主类 Application.java,但结果相同(404)。跨度>
  • 编辑:我通过右键单击 Application.java Run As java 应用程序进行了尝试。在端口 8080 (Tomcat) 上它可以工作。所以我认为问题在于Weblogic构建项目时没有考虑Maven依赖。
  • 您使用的是什么版本的 WebLogic?您是否使用 JDK 8 运行它? 404 可能意味着应用程序根本没有在 WebLogic 中安装,但您会在日志中看到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 2014-10-07
  • 2015-04-30
相关资源
最近更新 更多