有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

连接Java Rest服务

我目前正在尝试创建一个简单的Spring Boot应用程序,我可以从我的Postman应用程序向rest端点发送请求,该应用程序将对移交的数据执行基本操作,并在响应中返回结果

下面是我的RestService类,其中一个端点返回一条消息,表示它已连接到:

package TestRestService.TestRestService;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/testApplication")
@Consumes({ "application/json" })
@Produces({ "application/json" })
public class RestServiceTest {

    @GET
    @Path("/hitme")
    @Produces({ "application/json" })
    public Response getServerInfo(){
         String message = "Hit the end point";
         return Response.ok(message).build();
    }
 }

这是我的主要课程:

 @SpringBootApplication
 public class App {
      public static void main( String[] args ){
           SpringApplication.run(App.class, args);
      }
 }

我有一个index.html包含一个简单的标题和段落,显示在服务器上运行应用程序的时间。 我最初有一个网站。但是这阻止了我连接到服务中的任何页面,所以我删除了它,现在我可以访问索引了。html页面,但据我所知没有其他要点

当我通过邮递员或STS连接到http://localhost:8080/TestRestService/testApplication/hitme时,我得到:

hitme

有人能告诉我应该怎么做才能连接到这些rest端点吗


共 (2) 个答案

  1. # 1 楼答案

    我会尽力帮忙的 你用的是弹簧靴吗

    你把依赖关系放对了吗? 应该是这样的:

    <?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>com.test</groupId>
     <artifactId>test1</artifactId>
     <version>1.0.0</version>
     <packaging>war</packaging>
    
     <name>test1</name>
     <description>Project for test</description>
    
     <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.8.RELEASE</version>
      <relativePath /> <!-- lookup parent from repository -->
     </parent>
    
     <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
     </properties>
    
     <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-test</artifactId>
       <scope>test</scope>
      </dependency>
     </dependencies>
    
     <build>
      <plugins>
       <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
       </plugin>
      </plugins>
     </build>
    
    
    </project>
    

    控制器应该是这样的:

    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TestController {
    
      @RequestMapping(value = { "/api/hitme" }, method = { RequestMethod.GET },
          produces = { MediaType.APPLICATION_JSON_VALUE })
      public String test() throws Exception {
        // your code here
        return "I'M HIT!!";
      }
    
    }
    

    然后在应用程序中定义它。src/main/resources中的yml

    server:
      port: 9999
      context-path: /test
      display-name: test-service
    

    然后作为启动应用程序运行并尝试localhost:9999/test/api/hitme 如果想在ApacheTomcat容器中运行它,只需运行mvn clean install复制/target中生成的war并复制到tomcat中的webapps文件夹。跑,测试,瞧

    希望有帮助

  2. # 2 楼答案

    如果作为Spring Boot应用程序或java应用程序运行,则使用localhost:8080/testApplication/hitme;如果在war文件中运行,则使用localhost:8080/App_context/testApplication/hitme访问服务URI

    这里app_context是您的应用程序上下文