2016-04-28 42 views
1

我開始學習春天后備箱2天前,我讀了很多文章,但不幸的是預期它不工作(我的是一個Ruby Rails開發,對我來說學習Java有點困難;))春天開機/ MongoDB的:期待有不同的URL爲GET和DELETE

我想創建一個「簡單的」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> { 

} 

我希望管理以下網址的能力:

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(/標籤/:ID),我有這樣的錯誤消息 不支持請求方法'DELETE'

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

你知道我在做什麼錯?

謝謝

回答

0

我分享其工作通過重命名標記

我的主類我的示例代碼

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); 
} 

與樣品上面的代碼就可以開始春季啓動代碼,當你打http://localhost:8080/tag你可以看到所有的標籤,同樣,你可以通過點擊網址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路徑

0

您需要在mongoTemplate中自動導入Spring引導。有關於這個主題相當多的線程,你可以找到如何在SO配置此所以你需要尋找的答案就在這裏:

@Autowired 
private MongoTemplate mongoTemplate; 

然後,你將需要創建方法和查詢您的MongoDB使用mongotemplate:

@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; 
    } 

這是一個進行刪除:

@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); 
    } 

點你的瀏覽器API端點,通常是http://127.0.0.1:8080/mymethod_fetch,你會在瀏覽器中查看結果。

+0

謝謝,我會盡快嘗試。 我找到了解決方法,但它非常難看。我覆蓋控制器中的方法。 – Michael

0

謝謝,我會盡快嘗試。

我發現了一個解決方法,但它是非常hugly。我覆蓋控制器中的方法。這裏PetController一個例子:

@Autowired 
     private PetRepository petRepository; 

     @RequestMapping(value = "/pet/{id}", method = RequestMethod.GET) 
     @ResponseBody 
     public Pet findOneRemapped(@PathVariable("id") String id) { 
      return petRepository.findOne(id); 
     } 
+0

嗨。是的,您可以使用該方法,但是使用庫類來完成您的工作意味着您有時無法自定義搜索,例如排序等。 – Simon