【问题标题】:Postman returning error message after query for a rest endpoint邮递员在查询休息端点后返回错误消息
【发布时间】:2019-10-11 15:05:28
【问题描述】:

收到错误消息,我已尽我所能排除故障,但它不起作用。我不断收到错误消息,可能是什么问题?

这里是控制器:

package com.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.Model.*;
import com.Service.UserAccountService;


@RestController
public class UserController {

    @Autowired
    UserAccountService userService;

    @RequestMapping(path = "/users/")
    public List<UserAccount> getUsers()
    {
        return userService.getUsers();
    }
}

这是Service接口和ServiceImpl:

package com.Service;

import java.util.List;

import com.Exceptions.UserNotFoundException;
import com.Model.UserAccount;


public interface UserAccountService {

    UserAccount save(UserAccount user) throws Exception;

    List<UserAccount> getUsers();

    UserAccount update(UserAccount user, int id) throws Exception;

    //UserAccount delete(UserAccount user) throws Exception;

    UserAccount userAccountByKey(int id) throws UserNotFoundException;
   }

这是服务实现的代码:

package com.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Exceptions.UserNotFoundException;
import com.Model.UserAccount;
import com.Repository.UserRepo;
import com.Service.UserAccountService;

@Service
public class UserAccountServiceImplementation implements UserAccountService {

@Autowired  
private UserRepo repo;


@Override
public List<UserAccount> getUsers() {
    // TODO Auto-generated method stub
    List<UserAccount> users = new ArrayList<>();
    repo.findAll().forEach(users::add);
    return users;
}

这是模型的代码:

package com.Model;

import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
//import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

@Entity
public class UserAccount {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    @NotNull
    private String userName;
    private String email;
    @Temporal(TemporalType.DATE)
    private Date dateCreated;
    @OneToMany(mappedBy = "userAccount", fetch = FetchType.EAGER)
    private Set <Transaction> transactions = new HashSet<>();


    public UserAccount(int id) {
        super();
        this.id = id;
    }


    public UserAccount(String userName, String email, Date dateCreated) {
        super();
        this.userName = userName;
        this.email = email;
        this.dateCreated = dateCreated;
    }


    public UserAccount(int id, String userName, String email, Date dateCreated) {
        super();
        this.id = id;
        this.userName = userName;
        this.email = email;
        this.dateCreated = dateCreated;
    }


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getDateCreated() {
        return dateCreated;
    }
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    public Set<Transaction> getTransactions() {
        return transactions;
    }
    public void setTransactions(Set<Transaction> transactions) {
        this.transactions = transactions;
    }


    @Override
    public int hashCode() {
        return Objects.hash(id,userName,email,dateCreated);

    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UserAccount other = (UserAccount) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder ("UserAccount{");
        sb.append("id=").append(id);
        sb.append(",username='").append(userName).append('\'');
        sb.append(",email='").append(email).append('\'');
        sb.append(",date='").append(dateCreated).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

这是我的 application.properties 数据:

  spring.h2.console.enabled=true
  spring.datasource.platform=h2
  spring.datasource.driverClassName=org.h2.Driver
  spring.datasource.url=jdbc:h2:mem:mojec
  spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  spring.jpa.hibernate.ddl-auto=update
  spring.datasource.data=classpath:data.sql

这让我很担心。

【问题讨论】:

  • 错误信息是什么?
  • 请提供您收到的错误消息。
  • @JRK 错误信息:“错误”:“未找到”,
  • @HarryManoharan 错误信息是:“错误”:“未找到”,

标签: java spring hibernate spring-boot jpa


【解决方案1】:

RestController 从未由 Spring 配置,因为 Spring Boot 的组件扫描无法检测到它(事实上,您的其他 bean 类也不会被扫描)。这里的问题是你有一个这样的包结构:

com.Wallet
  - WalletApplication.java
com.Controller
  - UserController.java
com.Service
  - UserAccountServiceImpl.java

现在,mainClass 在包com.Wallet 中,它会扫描该包(或com.Wallet 中的子包)中的bean 定义。所以:

  • 要么把com.Controllercom.ServiceImpl等所有的包都搬走。里面com.Wallet,即-
com.Wallet
   |
   |- WalletApplication.java
   |
   |- Controller
   |  |
   |  |- UserController.java
   | 
   |- Service
      |
      |- UserAccountServiceImpl.java
  • 或者,在你的主类中使用@ComponentScan -
@EnableJpaRepositories
@SpringBootApplication
@ComponentScan(basePackages = { "com.Controller", "com.ServiceImpl", .. })
public class WalletApplication {

    public static void main(String[] args) {
        SpringApplication.run(WalletApplication.class, args);
    }
}

我建议第一个选项,因为它为您的应用程序提供了一个更简洁的包结构。

【讨论】:

  • 非常感谢当我更改包的名称并添加基本包时它起作用了,但我得到的其余 GET 方法的结果为空。只有两个像这样的数组括号 [] 。我需要添加 Jar 文件吗?
  • 数据库中是否有符合GET 端点标准的正确数据?
【解决方案2】:

首先需要检查包结构。例如,如果主类在 com.example 中,则其他类将遵循 com.example.controller、com.example.model 等。@ComponentScan 将扫描 com.example 之后的基础包..

【讨论】:

    【解决方案3】:

    您的请求映射需要像这样指定httpmethod:

    @RequestMapping(value= "/users", method=RequestMethod.GET)

    【讨论】:

    • 我已经添加了 method.RequestMethod.GET 但它仍然在邮递员上抛出“错误”:“未找到”
    • RequestMapping 默认使用RequestMethod.GET
    【解决方案4】:

    不要直接从控制器访问接口。通过服务层访问它。

    您应该创建服务类的实例而不是接口类。

    改变

         @Autowired
         UserAccountService userService;
    

        @Autowired
        UserAccountServiceImplementation userService;
    

    【讨论】:

    猜你喜欢
    • 2014-03-21
    • 2019-04-24
    • 2019-07-21
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多