我有Spring Boot和Jersey的应用程序。这里是pom.xml:
<?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>cat.amb.parkride</groupId>
<artifactId>parkride</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>parkride</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.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>com.wordnik</groupId>
<artifactId>swagger-annotations_2.10</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<!-- Mandatory dependencies for using Spock -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<!-- Mandatory dependencies for using Spock -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</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>
<!-- Mandatory dependencies for using Spock -->
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- Mandatory dependencies for using Spock -->
</plugins>
</build>
</project>
这是主要课程:
package cat.amb.parkride;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import cat.amb.parkride.adapter.rest.BasicRest;
import cat.amb.parkride.config.JerseyConfig;
@SpringBootApplication
//@ComponentScan(basePackages = {"cat.amb.parkride", "cat.amb.parkride.adapter.rest"})
public class ParkRideApplication {
private final Logger logger = LoggerFactory.getLogger(ParkRideApplication.class);
public static void main(String[] args) {
SpringApplication.run(ParkRideApplication.class, args);
}
}
这是我对Jersey的配置:
package cat.amb.parkride.config;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import cat.amb.parkride.adapter.rest.AuthRest;
import cat.amb.parkride.adapter.rest.TestRest;
import cat.amb.parkride.adapter.rest.UserRest;
@Configuration
@Component
@ApplicationPath("/parkride/rest")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig(ObjectMapper objectMapper) {
registerEndPoints();
register(LoggingFilter.class);
}
private void registerEndPoints() {
register(TestRest.class);
register(AuthRest.class);
register(UserRest.class);
}
}
这是Rest课程:
package cat.amb.parkride.adapter.rest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Consumes({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
@Produces({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
public class TestRest extends BasicRest {
private final Logger logger = LoggerFactory.getLogger(TestRest.class);
@Path("/test/v1/test")
@GET
public Response test() throws Exception {
logger.debug("Estamos dentro!!");
return getResponse("OK", null, HttpServletResponse.SC_OK);
}
@Path("/parkride/rest/test/v1/test")
@GET
public Response test2() throws Exception {
logger.debug("Estamos dentro!!");
return getResponse("OK", null, HttpServletResponse.SC_OK);
}
}
包的结构是:
cat.amb.parkride 适配器 道 休息 TestRest.java 公地 配置 域 ParkRideApplication.java
在每次调用此方法时,我都可以在日志中看到:
2017-05-08 18:37:42.110 INFO 8412 --- [ restartedMain] cat.amb.parkride.ParkRideApplication : Started ParkRideApplication in 2.219 seconds (JVM running for 70.125)
2017-05-08 18:37:45.275 INFO 8412 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter : 1 * Server has received a request on thread http-nio-8080-exec-1
1 > GET http://localhost:8080/parkride/rest/test/v1/test
1 > accept: */*
1 > accept-encoding: gzip, deflate, br
1 > accept-language: es,en;q=0.8,ca;q=0.6
1 > cache-control: no-cache
1 > connection: keep-alive
1 > content-type: application/json; charset=UTF-8
1 > host: localhost:8080
1 > postman-token: 34696faf-d1e2-fc6b-28d7-b2db672f2578
1 > user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3091.0 Safari/537.36
2017-05-08 18:37:45.275 INFO 8412 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter : 1 * Server responded with a response on thread http-nio-8080-exec-1
1 < 404
2017-05-08 18:37:45.276 INFO 8412 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-05-08 18:37:45.276 INFO 8412 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-05-08 18:37:45.285 INFO 8412 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 9 ms
但是我可以进入REST方法,好像泽西岛接受了请求但是没有执行方法
答案 0 :(得分:1)
需要使用@Path
注释的Jersey资源类。这是他们被称为root资源的唯一方式,即使您注册它们也是如此。他们仍然需要注释。您的TestRest
没有这个。如果它在BaseRest
课程上,则无法正常工作。它需要在已注册的具体实现上。
关于如何构建最终路线是基于@ApplicationPath
,资源类上的@Path
和@Path
的组合在资源方法上。如果资源方法没有@Path
,那么该路由将是资源类的路由&#39;路径。例如
@ApplicationPath("/api")
public class AppConfig extends ResourceConfig {}
@Path("/animals")
public class AnimalsResource {
@GET
public List<Animal> getAll() {}
@Path("/{id}")
pubic Animal getOne(@PathParam("id") int id) {}
}
GET /api/animals
转到AnimalsResource.getAll
GET /api/animals/1
转到AnimalsResource.getOne