我按照本教程关于数据访问: http://spring.io/guides/gs/accessing-data-rest/
所以,我有这个结构
http://i.stack.imgur.com/wUnrx.png
"项目"是模型
package Serveur;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String mail;
private String password;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
接口" ProjectRepository",由" @ RepositoryRestResource"直接实现。注释
包Serveur;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "jsonProjects", path = "jsonProjects")
public interface ProjectRepository extends CrudRepository<Project, Integer> {
List<Project> findById(@Param("name") String name);
}
&#34; Home Controller&#34;包含@RequestMapping Annotation并将用户重定向到&#34; index&#34;页面:
package Serveur;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping("/")
public String index()
{
return "index";
}
}
但是,当我连接到localhost:8080 /时,索引页面不显示,但是:
{
_links: {
jsonProjects: {
href: "http://localhost:8080/jsonProjects{?page,size,sort}",
templated: true
}
}
}
答案 0 :(得分:0)
问题是@RepositoryRestResource注释绑定到&#34; /&#34;。要解决您的问题,请编辑Application.java并添加此功能:
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
super.configureRepositoryRestConfiguration(config);
try {
config.setBaseUri(new URI("/api"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
您的HomeController不会绑定到&#34; /&#34;您的API将在&#34; / api&#34;
上提供答案 1 :(得分:-1)
以下是答案:
默认情况下,所有Repository搜索功能都位于搜索关键字之后。因此,在您的情况下,URL将类似于:
http://localhost:PORT/APP/jsonProjects/search/findById?name=NAME
请参阅我的工作示例和博客:
http://sv-technical.blogspot.com/2015/11/spring-boot-and-repositoryrestresource.html
https://github.com/svermaji/Spring-boot-with-hibernate-no-controller
希望这有帮助。