Home > Back-end >  How to get select data in json format in Spring Boot
How to get select data in json format in Spring Boot

Time:10-19

Repository

@Repository
public interface UserDao extends JpaRepository<User, Long> {
    @Query(value = "SELECT ss.name AS name, \n"  
            " SUM(pp.amount) AS amount, \n"  
            " REPLACE(ss.name, 'SomeName', 'DifferentName') AS cname \n"  
            " FROM payments AS pp\n"  
            " INNER JOIN auth_user AS au ON au.id = pp.creator_id\n"  
            " INNER JOIN services AS ss ON ss.id = pp.service_id\n"  
            " WHERE\n"  
            " pp.created_dt >= '2021-09-28' AND pp.created_dt < '2021-09-29' \n"  
            " AND ss.name = 'SomeName' AND pp.status = 'SUCCESS' \n"  
            " GROUP BY ss.name\n" // 
             , nativeQuery = true)
    List<User> findAll();
}

My Entity

@Entity
//@Table(name = "payment")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    //@Column(name = "name")
    private String name;

    //@Column(name = "amount")
    private long amount;

   // @Column(name = "name")
    private String cName;

    public User(
            @JsonProperty("name") String name,
            @JsonProperty("amount")long amount,
            @JsonProperty("cName")String cName) {
        this.name = name;
        this.amount = amount;
        this.cName = cName;
    }

    public User(long id, String name, long amount, String cName) {
        this.id = id;
        this.name = name;
        this.amount = amount;
        this.cName = cName;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public long getAmount() {
        return amount;
    }

    public String getcName() {
        return cName;
    }

    @Override
    public String toString() {
        return "User{"  
                "id="   id  
                ", name='"   name   '\''  
                ", amount="   amount  
                ", cName='"   cName   '\''  
                '}';
    }
}

Service

@Service(value = "userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    private List<SimpleGrantedAuthority> getAuthority() {
        return Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"));
    }

    public List<User> findAll() {
        List<User> list = new ArrayList<>();
        userDao.findAll();
        return list;
    }

    @Override
    public User save(User user) {
        return userDao.save(user);
    }
}

Controller

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public List<User> getAllUsers() {
        return userService.findAll();
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public List<User> listUser() {
        return userService.findAll();
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public User create(@RequestBody User user) {
        return userService.save(user);
    }

                                                                                                                  

I'm a beginner and I want to write a simple rest service that will take data from my request to the repository and output it in json format to localhost: 8080. Nothing works for me. Help me please. How should I implement this. I tried to do it myself, but I get a 404 error I know that the repository method needs to be called in the controller. But most likely I'm doing something wrong ... I have listed all the classes. In Postman, nothing is displayed for me

CodePudding user response:

Add @Override above public List User findAll() method in UserServiceImpl class

Try,

http://localhost:8080/users/users

and

http://localhost:8080/users/user

Both will give the same response.

  • Related