我已经使用Spring Boot(版本1.3.6.RELEASE)实现了REST API,该API可以按预期工作。但是,我想向我的应用程序添加JSON API支持。
问题是,当尝试在Katharsis资源上执行GET时,我得到404。
pom.xml中的依赖关系如下所示(我也使用Spring Security):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</exclusion>
</exclusions>
</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-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
</dependency>
<!--SpringFox dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dumbster</groupId>
<artifactId>dumbster</artifactId>
<version>1.6</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.katharsis</groupId>
<artifactId>katharsis-spring</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9</version>
</dependency>
</dependencies>
我的应用程序初始化类如下:
@SpringBootApplication
@Configuration
@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
@EnableSwagger2
@ComponentScan(basePackages = "com.myapp")
@Import({KatharsisConfigV2.class})
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
资源类:
@ApiModel(description = "")
@JsonApiResource(type = "foo-data")
public class FooData extends DomainRepresentation {
@JsonApiId
private String id = null;
@JsonProperty
private String name = null;
// .... getters & setters
}
ResourceRepository类:
@JsonApiResourceRepository(FooData.class)
@Component
public class FooDataKatApi extends MyApi {
@Autowired
private FooDataService fooDataService;
@JsonApiFindOne
public FooData foodataIdGet(String id, QueryParams requestParams) {
FooData result = fooDataService.getFooDataById(id);
return result;
}
}
如果我添加Spring
@ResourceMapping
,则返回数据,但没有添加Katharsis之类的附加内容,例如“link”等。这似乎是Katharsis并未接管API调用,而是由Spring处理。任何帮助,将不胜感激。
-编辑-
FooDataService如下:
@Service
public class FooDataService extends MyService {
@Autowired
private FooDataRepository fooDataRepository;
public FooData getFooDataById(String id) {
FooDataEnt fooDataEnt = fooDataRepository.findByFooDataId(id);
if (fooDataEnt != null) {
return convertFooDataEntToFooData.apply(fooDataEnt);
}
throw new NotFoundException("Foo not found");
}
}
FooDataRepository扩展了Spring的PagingAndSortingRepository。
application.yaml中的Katharsis配置:
katharsis:
resourcePackage: com.my.package
domainName: http://localhost:8080
pathPrefix: /api/kat
The request and response from PostMan
-更新1-
显然,katharsis.pathPrefix的配置值不正确。 Web应用程序的上下文根为“/api",katharsis.pathPrefix的值应仅为” / kat“。
我的下一个问题是,在处理请求的同时抛出RepositoryNotFoundException,而已找到Resource类。
最佳答案
我通过调试代码并发现我的Katharsis配置不正确来解决了这个难题。 Katharsis仅选择资源类,而不选择资源存储库类。当我将katharsis.resourcePackage更改为资源存储库所在的更高级别时,问题得到解决,Kathasis挑选了所有相关的带注释的类。