我正在使用Spring引导中的Rest公开我的服务。我首先通过使用cxf jaxrs依赖关系以及其他的spring rest依赖关系来以两种不同的方法来公开我的服务。在第一种方法中,我在邮递员中运行服务时遇到404错误。但是在第二种方法中,我得到了我的输出。请帮帮我。
使用cxf jaxrs的第一种方法
@Path("/locations")
public interface LocationRestController {
@GET
public List<Location> getLocations();
}
******************************************
@RestController
public class LocationRestControllerImpl implements LocationRestController {
@Autowired
LocationRepos repos;
@Override
public List<Location> getLocations() {
return repos.findAll();
}}
{
"timestamp": "2018-09-17T03:36:05.283+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/locationweb/locations"
}
使用spring内置依赖项的第二种方法
@RestController
@RequestMapping("/locations")
public class LocationRestControllerImpl {
@Autowired
LocationRepos repos;
@GetMapping
public List<Location> getLocations() {
return repos.findAll();
}}
Pom文件
<?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.project.abhishek</groupId>
<artifactId>student</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>student</name>
<description>Student DAL</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
"pom.xml" <version>2.1.0.M2</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
答案 0 :(得分:1)
请参考以下代码以正确实施
public interface LocationRestController {
public List<Location> getLocations();
}
******************************************
@RestController(value="/locationweb/locations")
public class LocationRestControllerImpl implements LocationRestController {
@Autowired
LocationRepos repos;
@Override
@GetMapping
public List<Location> getLocations() {
return repos.findAll();
}}
{
答案 1 :(得分:0)
此行可能是问题
$this->app->extend(Mailer::class, function ($mailer) {
return new MailerDecorator($mailer);
});
以第一种方法从班级中删除@RestController
public class LocationRestControllerImpl implements LocationRestController {
答案 2 :(得分:0)
尝试以下示例, https://dzone.com/articles/spring-boot-building-restful-web-services-with-jersey
很可能您缺少Spring / Jersey集成或Jersey配置。
添加用于春季启动的Jersey配置以了解Jersey。
为方便起见,下面是“ Jersey Configuration”的代码。
@Configuration
@ApplicationPath("rest")
public class Config extends ResourceConfig {
public Config() {
}
@PostConstruct
public void setUp() {
register(Controller.class);
}
}
答案 3 :(得分:0)
请确保您拥有CXF configuration
并将其设置为服务Bean,就像您使用@RestController
一样,实际上并没有实现CXF
,因此要通过CXF
处理请求,您需要配置CXF
服务器。
@Configuration
public class CXFConfig {
@Autowired
private Bus bus;
@Bean
public Server rsServer() {
final JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
endpoint.setBus(bus);
endpoint.setAddress("/");
endpoint.setServiceBeans(Arrays.asList(LocationRestController ()));
return endpoint.create();
}
@Bean
public LocationRestController locationController() {
return new LocationRestControllerImpl();
}
}
注意:无需实现控制器,您可以直接拥有控制器。