【问题标题】:When i'm trying to test rest-api is not working any methods all methods in postman is getting back and not found当我尝试测试 rest-api 没有使用任何方法时,邮递员中的所有方法都在返回并且找不到
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 是的,我有 Starter 课程,但我忘记在此处发布。问题不存在,因为应用程序启动只是方法没有返回任何我提到的东西。例如:我正在测试第一个方法。POST 添加一些用户,但它不起作用。
  • 没有达到方法。控制器方法未识别。可能的原因是您可能已在控制器类的同一层次结构中添加了 Starter 类。它必须在控制器文件夹之上。
  • 是的,现在我明白了,basepackage 和层次级别不太好,非常感谢你,现在它可以工作了:) 祝福你@Naveed S
猜你喜欢
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
相关资源
最近更新 更多