2017-08-07 64 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

相关问题