Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接Java Rest服务_Java_Rest_Spring Boot - Fatal编程技术网

连接Java Rest服务

连接Java Rest服务,java,rest,spring-boot,Java,Rest,Spring Boot,我目前正在尝试创建一个简单的Spring引导应用程序,我可以从我的Postman应用程序向rest端点发送请求,该应用程序将对移交的数据执行基本操作,并在响应中返回结果 这是我的RestService类,其中一个端点返回一条消息,表示它已连接到: package TestRestService.TestRestService; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import

我目前正在尝试创建一个简单的Spring引导应用程序,我可以从我的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
,其中包含一个简单的标题和段落,显示在服务器上运行应用程序的时间。 我最初有一个web.xml,但这使我无法连接到服务中的任何页面,所以我删除了它,现在我可以访问index.html页面,但我知道没有其他点

当我通过邮递员或STS连接到时,我得到:


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

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


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

正如您提到的,您正在使用Spring boot,那么对于REST服务,请使用RestController注释

    @RestController
    @RequestMapping("/restServiceTest")
    public class RestServiceTest {

      @RequestMapping(value = "/test", method = RequestMethod.GET)
       public @ResponseBody ResponseEntity<String> test() {
              return new ResponseEntity<String>("Success",HttpStatus.OK)
       }
     }
@RestController
@请求映射(“/restServiceTest”)
公共类RestServiceTest{
@RequestMapping(value=“/test”,method=RequestMethod.GET)
public@ResponseBody ResponseEntity测试(){
返回新的响应状态(“成功”,HttpStatus.OK)
}
}

希望这有帮助


好的,找到问题。您使用的是JAX-RS规范,没有任何提供程序,如Jersey、CXF。我修改了pom.xml并使用SpringRestController实现REST服务。请使用下面的,然后测试从浏览器或邮递员命中URI,你会得到响应。下面是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>
}我会试着帮你 你用的是弹簧靴吗

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

<?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中的application.yml中定义它

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


希望能有所帮助。

调用“`您的项目/war文件的名称是什么?@ByeBye在Har的回答中这样做了,但没有运气。@Jens我的项目名为TestRestServices,我在STS中调用“在服务器上运行”,但仍然没有运气“源服务器找不到目标资源的当前表示形式,或者不愿意透露是否存在该表示形式。”请尝试将@Consumes({”application/json“})注释添加到@GET@Path(“/hitme”)@Products({”application/json“})公共响应getServerInfo(){String message=“点击终点“返回Response.ok(message.build();}}}您在使用Eclipse吗?如果是,则rt单击App.java并单击RunAs->java应用程序。然后调用localhost:8080/testApplication/hitme。我右键单击runas->java应用程序,它会要求选择java应用程序。我选择“SpringApplication-org.springframework.boot”,然后它会给我“java.lang.IllegalArgumentException:源不能为空”。然后我选择了“App-packagename.packagename”,然后它启动应用程序并立即关闭。可能的帮助?=“正在关闭org.springframework.context.annotation。AnnotationConfigApplicationContext@2473b9ce:启动日期[Wed Nov 22 07:19:46 GMT 2017];上下文层次结构的根“Rt”。单击项目上的App.java,然后作为java应用程序或Spring Boot应用程序运行。不幸的是,我仍然可以“源服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式。“您是否安装了eclipse的sts插件,在spring boot中不需要web.xml。用于在没有web.xml的sts中生成spring boot projectI am。这是我以前查看index.html页面的能力。我添加了两个图像,请看一看,创建一个与SpringWebDependency相关的新项目,因此我已经将您的工作实施到一个新项目中,我似乎遇到了与我在与HarKrishan的帖子中解释的相同的问题。应用程序启动,然后立即再次关闭。这是我的控制台输出。-。不幸的是,我完全复制了您的代码,完全替换了pom.xml和Rest类以匹配您的代码,然后运行了maven更新,但没有成功。这是更新的控制台日志输出-请从中复制包含pom.xml的代码,然后从浏览器中点击进行测试。我在本地测试了代码,它正在工作。我已经用你的pom覆盖了我的pom,然后运行了一个maven->下载源代码&更新项目。然后右键单击App.java并运行as->java应用程序,然后转到localhost:8080/testApplication,错误仍然是404,并显示消息“源服务器未找到目标资源的当前表示形式,或者不愿意透露存在该表示形式。”。如果它对您有效,但对我无效,这可能是tomcat的问题吗?当我从pom.xml评论tomcat插件时,我认为没有任何东西会导致问题。请分享您的pom.xml。问题似乎出在项目结构上。请按照以下步骤创建新的Maven项目:使用File->new->Other->Maven project->(选择选项create a simple project(跳过原型选择)),然后提供groupid、工件和版本详细信息。一旦创建了Maven项目,然后将pom.xml和类复制到该项目中,然后运行。
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!!";
  }

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