2017-08-07 63 views
1

我已經定義了一個@Projection我的數據春春實體RepositoryRestController。當我做GET請求時,一切都按預期返回。但是當我做POST請求時,投影將不起作用。按照上面提供的示例,「地址」在鏈接下顯示爲URL,並未按照GET請求的方式公開。描述<a href="http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.excerpting-commonly-accessed-data" rel="nofollow noreferrer">here</a></p> <p>出於同樣的原因,因爲描述excerptProjection

如何以同樣的方式獲得它?

我用@RepositoryRestController創建了一個類,我可以在這裏找到POST方法。如果我簡單地返回實體,它是沒有鏈接的。如果我將它作爲資源返回,鏈接就在那裏,但「地址」也是一個鏈接。如果我從控制器中刪除GET方法,則默認行爲如上所述。

UPDATE

我的實體是相同的描述hereAB和除了我SuperClass沒有取我@ManyToOne 我的控制器定義看起來是這樣的:

@RepositoryRestController 
public class BRepositoryRestController { 

    private final BRepository bRepository; 

    public BRepositoryRestController(BRepository bRepository) { 
    this.bRepository = bRepository; 
    } 


    @RequestMapping(method = RequestMethod.POST, value = "/bs") 
    public 
    ResponseEntity<?> post(@RequestBody Resource<B> bResource) { 
    B b= bRepository.save(bResource.getContent()); 
    BProjection result = bRepository.findById(b.getId()); 
    return ResponseEntity.ok(new Resource<>(result)); 
    } 
} 

我的資料庫看起來像這樣:

@RepositoryRestResource(excerptProjection = BProjection.class) 
public interface BRepository extends BaseRepository<B, Long> { 
    @EntityGraph(attributePaths = {"a"}) 
    BProjection findById(Long id); 
} 

我的投影是這樣的:

@Projection(types = B.class) 
public interface BProjection extends SuperClassProjection { 
    A getA(); 
    String getSomeData(); 
    String getOtherData();  
} 

而且SuperClassProjection看起來是這樣的:

@Projection(types = SuperClass.class) 
public interface SuperClassProjection { 
    Long getId(); 
} 

回答

1

在定製@RepositoryRestController POST方法也應該返回的投影。例如:

@Projection(name = "inlineAddress", types = { Person.class }) 
public interface InlineAddress { 
    String getFirstName(); 
    String getLastName(); 
    @Value("#{target.address}") 
    Address getAddress(); 
} 

public interface PersonRepo extends JpaRepository<Person, Long> { 
    InlineAddress findById(Long personId); 
} 

@PostMapping 
public ResponseEntity<?> post(...) { 
    //... posting a person 
    InlineAddress inlineAddress = bookRepo.findById(person.getId()); 
    return ResponseEntity.ok(new Resource<>(inlineAddress)); 
} 

UPDATE

我已經糾正了我上面的代碼和代碼的問題:

@RepositoryRestResource(excerptProjection = BProjection.class) 
public interface BRepository extends CrudRepository<B, Long> { 
    BProjection findById(Long id); 
} 

@Projection(types = B.class) 
public interface BProjection { 
    @Value("#{target.a}") 
    A getA(); 
    String getSomeData(); 
    String getOtherData(); 
} 

然後一切工作正常。

POST請求體:

{ 
    "name": "b1", 
    "someData": "someData1", 
    "otherData": "otherData", 
    "a": { 
     "name": "a1" 
    } 
} 

響應體:

{ 
    "a": { 
     "name": "a1" 
    }, 
    "someData": "someData1", 
    "otherData": "otherData", 
    "_links": { 
     "self": { 
      "href": "http://localhost:8080/api/bs/1{?projection}", 
      "templated": true 
     } 
    } 
} 

見工作example

+0

嗯,我的倉庫被延長CRUDRepository。我得到一個錯誤:'查詢指定的連接提取,但獲取的關聯的所有者沒有出現在選擇列表中......' – JamesLinux

+0

@JamesLinux將你的模型和回購代碼添加到你的問題 - 這將是我更容易糾正我的代碼。但主要想法是返回控制器方法中的投影。 – Cepr0

+0

這和示例中的一樣。我定義了一個@ManyToOne(可選= false,cascade = CascadeType.PERSIST)'。我的預測也是一樣的。 – JamesLinux

相關問題