gpt4 book ai didi

java - Katharsis 结合 Spring Boot

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:30 26 4
gpt4 key购买 nike

我已经使用 Spring Boot(版本 1.3.6.RELEASE)实现了 REST 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
}

资源库类:

@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 添加,如“链接”等。就好像 API 调用没有被 Katharsis 接收,但它已被处理到 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 类已经找到。

最佳答案

我在任何地方都看不到您的 foo 数据服务实现。但是,我 99% 确定您没有传递 json api 接受类型。

Accept: application/vnd.api+json

关于java - Katharsis 结合 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39662684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com