java - 连接 Java Rest 服务

标签 java rest spring-boot

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

这是我的 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();
    }
 }

这是我的主要类(class):

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

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

当我连接到http://localhost:8080/TestRestService/testApplication/hitme时通过 postman 或在 STS 我得到:

hitme

有人可以建议我应该做什么才能连接到这些休息端点吗?

最佳答案

好的,找到问题了。您正在使用 JAX-RS 规范,而没有任何提供商(例如 Jersey、CXF)。我修改了你的pom.xml并使用Spring RestController来实现REST服务。请使用下面的内容,然后测试点击 URI http://localhost:8080/testApplication/来自浏览器或 postman ,您将收到响应。这是pom.xml

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

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api 
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
</dependency>
-->
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- 
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat6-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
             -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

这是 RestController 类:

package com.resources;

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 SpringRestController {

@RequestMapping(method = { RequestMethod.GET }, value = { "/testApplication" }, consumes = MediaType.ALL_VALUE, produces = MediaType.ALL_VALUE)
public String getServerInfo() {
    System.out.println("I got hit");
    String message = "Hit the end point";
    return message;
}

}

关于java - 连接 Java Rest 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47428177/

相关文章:

java - 如何根据 JCheckBox 的状态启用/禁用我的 JTextField?

java - 如何初始化对象数组?

java - 在 JAR 的同一文件夹中进行 java 日志记录

java - 两个 jar 具有相同的包名、相同的类名和相同的方法名

java - Hystrix状态未暴露在/health下

java - Jpa 存储库保存类型参数 'S' 的推断类型 'S' 不在其范围内

java - 使用嵌套 for 循环初始化 2D 数组并查找模式

rest - 证书颁发机构无效或不正确

java - 面向服务的架构spring-mvc

api - 在 REST Api 调用中批量更新集合的最佳实践