首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rest api调用POST API调用失败

Rest api调用POST API调用失败
EN

Stack Overflow用户
提问于 2021-08-27 19:56:00
回答 1查看 54关注 0票数 0

您好,我最近使用STS4创建了一个微服务post API调用,该调用已成功构建和部署。localhost:8080/demo/链接正在打开。但其下的其余映射失败,未找到404。我已经添加了请求映射和post映射,但仍然不能正常工作。有人能修复代码吗?

PFB代码:- POM:-

代码语言:javascript
复制
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <maven.test.skip> true</maven.test.skip>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

下面是swagger的配置:-

代码语言:javascript
复制
package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration  
//Enable Swagger  
@EnableSwagger2 
public class SwaggerConfig {
  
  @Bean  
  public Docket api() {  
     //creating constructor of Docket class that accepts parameter DocumentationType  
     return new Docket(DocumentationType.SWAGGER_2);  
  }
}

下面是rest控制器:

代码语言:javascript
复制
package com.example.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.MyResponse;
import com.example.demo.service.dao.DemoServiceDAO;
import com.example.demo.service.impl.DemoServiceImpl;

@RestController
@Validated
@RequestMapping(value ="/newcall", method = RequestMethod.POST)
public class DemoController {
  Logger logger =LoggerFactory.getLogger(DemoController.class);
  
  @PostMapping(value ="/newResp", headers ="Accept=Application/JSON", produces="Application/JSON")
  public MyResponse newResp(@RequestBody String myInput) {
    MyResponse myResponse = null;
    try {
      DemoServiceDAO demoServiceDAO = new DemoServiceImpl();
      logger.debug("Started getValue() method...");
      myResponse=demoServiceDAO.getValue(myInput);
      logger.debug("Completed getValue() method...");
      
    }
    catch(Exception e){
      logger.debug("Exception occurs",e);
      e.printStackTrace();
      System.out.println("Exception occurs");
    }       
    return myResponse;
  }
}

以下是应用程序:

代码语言:javascript
复制
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@ComponentScan(basePackages = "com.example.*")
@EnableSwagger2
public class DemoApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    Logger logger =LoggerFactory.getLogger(DemoApplication.class);
    SpringApplication.run(DemoApplication.class, args);
  }
}

这里是app.properties:-

代码语言:javascript
复制
    logging.file.name=opt/var/logs/demoService.log
    logging.file.path=opt/var/logs
    logging.level.web=DEBUG
    spring.jmx.default-domain=demo
    server.servlet.context-path=/demo
    management.endpoints.web.exposure.include=*
    management.endpoint.health.show-details=always
    
    springfox.documentation.swagger-ui.base-url=true
    management.endpoint.info.enabled: true
    springfox.documentation.swagger-ui.enabled=true
    springfox.documentation.open-api.enabled=true
    spring.security.user.name = user
    spring.security.user.password = password

来自http://localhost:8080/demo/的输出

代码语言:javascript
复制
     "_links" : {
       "profile" : {
          "href" : "http://localhost:8080/demo/profile"
        }
     }

完整的代码在github和日志中:

https://github.com/Sourav654/tempProject/tree/master/demo

EN

回答 1

Stack Overflow用户

发布于 2021-08-27 20:26:13

没有任何日志,我们只能猜测,但是看着你的控制器,我看到了一些奇怪的事情。你能试一下下面的方法吗?

代码语言:javascript
复制
@RestController
@Validated
@RequestMapping("newcall")
public class DemoController {
  Logger logger =LoggerFactory.getLogger(DemoController.class);
  
  @PostMapping("/newResp", consumes = "application/json", produces = application/json)
  public MyResponse newResp(@RequestBody String myInput) {
    MyResponse myResponse = null;
    try {
      DemoServiceDAO demoServiceDAO = new DemoServiceImpl();
      logger.debug("Started getValue() method...");
      myResponse=demoServiceDAO.getValue(myInput);
      logger.debug("Completed getValue() method...");
      
    }
    catch(Exception e){
      logger.debug("Exception occurs",e);
      e.printStackTrace();
      System.out.println("Exception occurs");
    }       
    return myResponse;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68958769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档