我目前正在尝试创建一个简单的Spring Boot应用程序,我可以将Postman应用程序的请求发送到休息端点,应用程序将对移交的数据执行基本操作,并在Response中返回结果。
这是我的 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页面,但没有其他我知道的点。
当我通过Postman或STS连接到http://localhost:8080/TestRestService/testApplication/hitme时,我得到:
任何人都可以告诉我应该做些什么才能连接到这些休息端点?
答案 0 :(得分:1)
好的,发现了这个问题。您使用的是JAX-RS Spec,没有像Jersey,CXF这样的提供商。我修改了你的pom.xml并使用Spring RestController来实现REST服务。请使用下面的内容,然后从浏览器或邮递员测试点击URI http://localhost:8080/testApplication/,您将收到回复。这是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;
}
}
答案 1 :(得分:0)
如果以Spring Boot App或java应用程序运行,则使用localhost:8080 / testApplication / hitme,如果在war文件中运行,则使用localhost:8080 / app_context / testApplication / hitme访问服务URI。
此处app_context是您的应用程序上下文。
答案 2 :(得分:0)
正如您所提到的,您正在使用Spring-boot,然后REST服务转到RestController注释。
.ToString("MMMM dd,yyyy", new CultureInfo("en-US"))
希望这有帮助!!
答案 3 :(得分:0)
我会尝试帮助 你使用弹簧靴吗?
你有没有把正确的依赖? 应该是这样的:<?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
如果你想在apache tomcat容器中运行它,只需运行mvn clean install
复制/ target中生成的war并复制到tomcat中的webapps文件夹。跑,测试和瞧。