[英]Spring boot / MongoDB : expecting to have different URL for GET and DELETE
I started to learn spring boot 2 days ago, I've read a lots of articles but unfortunately it does not work as expected (I'm a Ruby on Rails developper, it's a bit difficult for me to learn Java ;) ) 我2天前开始学习spring boot,我读了很多文章,但不幸的是它没有按预期工作(我是Ruby on Rails开发者,我学习Java有点困难;))
I want to create a "simple" REST application in order to create, get and delete a Tag class and I use mongoDB. 我想创建一个“简单”的REST应用程序,以便创建,获取和删除Tag类,我使用mongoDB。
My understanding is that I have to create a TagRepository file with this content: 我的理解是我必须使用以下内容创建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> {
}
I expect the ability to manage the following URLs: 我希望能够管理以下网址:
GET: http://localhost:8080/tags
GET: http://localhost:8080/tag/:id
DELETE: http://localhost:8080/tag/:id
Unfortunately I can only use 不幸的是我只能使用
GET: http://localhost:8080/tags
GET: http://localhost:8080/tags/:id
DELETE: http://localhost:8080/tags/:id
If I try the delete URL (/tag/:id) I have this error message Request method 'DELETE' not supported 如果我尝试删除URL(/ tag /:id)我有此错误消息请求方法'DELETE'不受支持
but if I use the delete URL (/tags/:id) it works fine. 但如果我使用删除URL(/ tags /:id),它可以正常工作。
Do you know what I'm doing wrong? 你知道我做错了什么吗?
Thank you 谢谢
You will need to autowire in your mongoTemplate into Spring boot. 您需要在mongoTemplate中自动装入Spring引导程序。 There are quite a few threads on this topic, you can find how to configure this on SO so you will need to search for the answer on here: 关于这个主题有很多线程,你可以在SO上找到如何配置它,所以你需要在这里搜索答案:
@Autowired
private MongoTemplate mongoTemplate;
Then you will need to create your method and query your mongodb using the mongotemplate: 然后你需要创建你的方法并使用mongotemplate查询你的mongodb:
@RequestMapping(value="/mymethod_fetch", method=RequestMethod.GET)
public List myMethod(@RequestParam String name) {
Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
query.with(new Sort(Sort.Direction.DESC, "_id"));
query.limit(10);
List<Object> obj = mongoTemplate.find(query, Object.class);
return obj;
}
This is the one for delete: 这是删除的一个:
@RequestMapping(value="/mymethod_item_delete", method=RequestMethod.DELETE)
public void myMethod(@RequestParam String name) {
Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
mongoTemplate.remove(query, Object.class);
}
Point your browser to the api endpoints, which is normally http://127.0.0.1:8080/mymethod_fetch and you will see the results in your browser. 将浏览器指向api端点,通常是http://127.0.0.1:8080/mymethod_fetch ,您将在浏览器中看到结果。
Thanks, I will try it soon. 谢谢,我会尽快尝试。
I found a workaround but it's very hugly. 我发现了一种解决方法,但它非常难看。 I overwrite the methods in the controller. 我覆盖了控制器中的方法。 Here an example for PetController: 这是PetController的一个例子:
@Autowired
private PetRepository petRepository;
@RequestMapping(value = "/pet/{id}", method = RequestMethod.GET)
@ResponseBody
public Pet findOneRemapped(@PathVariable("id") String id) {
return petRepository.findOne(id);
}
I am sharing my sample code which worked by renaming to tag 我正在分享我通过重命名为标记工作的示例代码
My main class 我的主要课程
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);
}
}
My Model Class 我的模型类
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;
}
}
and finally our repository class 最后我们的存储库类
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);
}
with sample above code you can start spring boot code and when you hit http://localhost:8080/tag you can see all tags and similarly you can retrieve single tag by hitting url http://localhost:8080/tag/:id and delete by http://localhost:8080/tag/:id 使用上面的代码示例,您可以启动Spring启动代码,当您点击http:// localhost:8080 / tag时,您可以看到所有标签,类似地,您可以通过点击URL来检索单个标签http:// localhost:8080 / tag /:id并通过http:// localhost:8080 / tag /:id删除
below is my pom which i have used to demonstrate the above code 下面是我用来演示上述代码的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>
With this code we can obtained your desired url paths 使用此代码,我们可以获得您想要的URL路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.