2014-01-08 79 views
0

我有春天HATEOAS工作訪問特定的資源,如Spring HATEOAS:如何發佈資源服務?

http://localhost:8080/user/1 

但我想也能宣傳服務網址:

http://localhost:8080/user 

舉例來說,如果你這樣做一個GET /,我想返回我宣傳的服務資源。現在唯一一個是/ auth。

@RequestMapping(value = "/", method = RequestMethod.GET) 
@ResponseBody 
public HttpEntity<AuthenticationResource> post() { 
    AuthenticationResource resource = new AuthenticationResource(); 
    resource.add(linkTo(methodOn(AuthenticationController.class).authenticate()).withSelfRel()); 
    return new ResponseEntity<AuthenticationResource>(resource, HttpStatus.OK); 
} 

@RequestMapping(value = "/auth", method = RequestMethod.POST, consumes = "application/json") 
@ResponseBody 
public void authenticate() { 
    //users.save(user); 
} 

目前,這不是因爲編譯linkTo不採取無效的說法,這我相信我的身份驗證方法的返回類型

我想是這樣的:

{"links":[{"rel":"someString","href":"http://localhost/auth"}]} 

如何在HATEOAS最佳實踐中完成這項工作?

回答

1

這個。

@RequestMapping(value = "/", method = RequestMethod.GET) 
@ResponseBody 
public HttpEntity<ResourceSupport> post() { 
    ResourceSupport resource = new ResourceSupport(); 
    resource.add(linkTo(methodOn(AuthenticationController.class).authenticate()).withRel("authenticate")); 
    return new ResponseEntity<ResourceSupport>(resource, HttpStatus.OK); 
} 

@RequestMapping(value = "/auth", method = RequestMethod.POST, consumes = "application/json") 
@ResponseBody 
public HttpEntity<AuthenticationResource> authenticate() { 
    AuthenticationResourceAssembler assembler = new AuthenticationResourceAssembler(); 
    AuthenticationResource resource = assembler.toResource(new Authentication()); 

    return new ResponseEntity<AuthenticationResource>(resource, HttpStatus.OK); 
} 
+0

這與您的問題沒有關係,但您應該注入一個Autowired AuthenticationResourceAssembler而不是在每個POST上創建... –