http post方法

e5njpo68  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(420)

我有一个restapi,部署在jbosseap7.1上。当我点击网址

http://localhost:8080/MyApp/group

在postman中,它给出“http method post is not supported by this url”错误,状态代码405。
当我在嵌入式tomcat服务器上部署这个api时,它运行得非常好。这是我的控制器

@RestController
public class RequestController {

    @Autowired
    private GroupService groupService;

    @PostMapping( "/group" )
    public GroupInfo fetchGroupInfo( @RequestBody GroupInfo groupInfo ) {

        long groupId = groupInfo.getGroupId();
        return groupService.getGroup( groupId );

    }

}

主要类别

@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
        return builder.sources(MyApp.class);
    }

}

pom.xml文件

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.my.package</groupId>
    <artifactId>MyApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MyApp</name>
    <packaging>war</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.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-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

    </dependencies>

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

</project>

部署没有显示错误消息。不过,它确实显示了以下警告

WARN  [org.jboss.weld.deployer] (MSC service thread 1-4) WFLYWELD0013: Deployment optumRx-0.0.1-SNAPSHOT.war contains CDI annotations but no bean archive was found (no beans.xml or class with bean defining annotations was present).

类文件在jboss的部署文件夹中丢失。你能告诉我,我错过了什么吗?

c86crjj0

c86crjj01#

看起来我错过了项目中的servlet api依赖项。在我添加了依赖项之后,一切都很正常。嵌入的tomcat服务器有这个jar,因此,当我在tomcat上部署时,不需要它。

yptwkmov

yptwkmov2#

关于路径:pom.xmlmyapp中的构造在url中没有说明路径,而是关于工件的名称。尝试使用http://localhost:8080/组(不带myapp)
关于其他:如果要在jboss上部署,必须在pom.xml中排除tomcat:

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

关于cdi警告-尝试在myapp类上使用@componentscan(basepackages=“your.package”)

相关问题