【发布时间】:2016-11-18 10:28:27
【问题描述】:
模型类
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
public String name;
@Column
public String lastName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getId() {
return id;
}
}
--存储库
public interface PersonRepository extends JpaRepository<Person, Long>{
}
--服务
public interface PersonServices {
Person addPerson(Person person);
Person findOne(Long id);
List<Person> findAll();
}
-- 实现服务接口的类
@Service
@Transactional
public class PersonServiesImpl implements PersonServices {
@Autowired
private PersonRepository personRepository;
@Override
@Transactional
public Person addPerson(Person person) {
return personRepository.save(person);
}
@Override
@Transactional(readOnly=true)
public Person findOne(Long id) {
return personRepository.findOne(id);
}
@Override
public List<Person> findAll() {
// TODO Auto-generated method stub
return personRepository.findAll();
}
}
控制器类
@RestController
@RequestMapping("/rest")
public class PersonController {
@Autowired
private PersonServices personServices;
@RequestMapping(value = "/user",
method = RequestMethod.POST)
public ResponseEntity<Person> savePerson(@RequestBody Person person){
return new ResponseEntity<Person>(personServices.addPerson(person),HttpStatus.CREATED);
}
@RequestMapping(value = "/user",
method = RequestMethod.GET)
public ResponseEntity<List<Person>> findAll(){
return new ResponseEntity<List<Person>>(personServices.findAll(),HttpStatus.OK);
}
@RequestMapping(value = "/user/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> getFromId(@PathVariable("id")Long id){
return new ResponseEntity<Person>(personServices.findOne(id),HttpStatus.OK);
}
}
--application.properties 文件
spring.datasource.url =jdbc:mysql://localhost:3306/managementsystem
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.profiles.active=dev
server.port=8080
server.sessionTimeout=30
spring.jpa.hibernate.ddl-auto=create-drop
tomcat.accessLogEnabled=false
tomcat.protocolHeader=x-forwarded-proto
tomcat.remoteIpHeader=x-forwarded-for
tomcat.backgroundProcessorDelay=30
问题是,例如,当我测试在数据库中保存一个人的方法时,邮递员返回的消息是 {timestamp : 1242424 , message : not found , path:/rest/user 。谁能帮助我,我是 Spring Boot Framework 的初学者如何解决这个问题?我检查了一些教程,但几乎是一样的,我不知道。我希望有人可以帮助我:)
【问题讨论】:
-
你能发布堆栈跟踪吗?
标签: java spring spring-mvc spring-boot spring-data