gpt4 book ai didi

java - Spring 启动/MongoDB : expecting to have different URL for GET and DELETE

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:53 25 4
gpt4 key购买 nike

两天前开始学习spring boot,看了很多文章,可惜并没有达到预期的效果(我是Ruby on Rails开发者,学Java对我来说有点困难;))

我想创建一个“简单”的 REST 应用程序,以便创建、获取和删除 Tag 类,并且我使用 mongoDB。

我的理解是我必须创建一个包含以下内容的 TagRepository 文件:

package com.petstore.repositories;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.web.bind.annotation.RequestMapping;

import com.petstore.models.Tag;

@RepositoryRestResource(collectionResourceRel = "tags", path="tags")
public interface TagRepository extends MongoRepository<Tag, String> {

}

我希望能够管理以下 URL:

GET: http://localhost:8080/tags
GET: http://localhost:8080/tag/:id
DELETE: http://localhost:8080/tag/:id

不幸的是我只能使用

GET: http://localhost:8080/tags
GET: http://localhost:8080/tags/:id
DELETE: http://localhost:8080/tags/:id

如果我尝试删除 URL (/tag/:id),我会收到此错误消息不支持请求方法“DELETE”

但如果我使用删除 URL (/tags/:id),它就可以正常工作。

你知道我做错了什么吗?

谢谢

最佳答案

我正在分享我的示例代码,该代码通过重命名为标签来工作

我的主课

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.web.config.EnableSpringDataWebSupport;

@SpringBootApplication
@EnableSpringDataWebSupport
public class SpringBootDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}

我的模型类

package com.example;

import org.springframework.data.annotation.Id;

public class Tag
{
@Id
String id;
String tag;

public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getTag()
{
return tag;
}
public void setTag(String tag)
{
this.tag = tag;
}
}

最后是我们的存储库类

package com.example;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;

@RepositoryRestResource(collectionResourceRel = "tags", path="tag")
public interface TagRepository extends MongoRepository<Tag, String>
{
@RestResource(path = "tags")
Page<Tag> findAll(Pageable pageable);

void delete(String tag);
}

使用上面的示例代码,您可以启动 Spring Boot 代码,当您点击 http://localhost:8080/tag 时您可以看到所有标签,同样您可以通过点击 url http://localhost:8080/tag/:id 来检索单个标签。并按 http://localhost:8080/tag/:id 删除

下面是我的 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.example</groupId>
<artifactId>MongoDB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>SpringBootDemo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
<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>

通过这段代码我们可以获得您想要的url路径

关于java - Spring 启动/MongoDB : expecting to have different URL for GET and DELETE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36926484/

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