繁体   English   中英

当我尝试测试rest-api不能正常工作时,邮递员中的所有方法都无法找到

[英]When i'm trying to test rest-api is not working any methods all methods in postman is getting back and not found

型号类别

@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:找不到,path:/ rest / user。 谁能帮我,我是Spring Boot Framework的初学者如何解决此问题? 我检查了一些教程,但是几乎不知道这样。 我希望有人可以帮助我:)

似乎没有扫描RestController和控制器方法。 您应该将Starter类(用@SpringBootApplication注释的类)放在文件夹层次结构中的控制器上方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM