1

I need to return a service info by id. I've tried different variations of writing my code but they all lead to java not seeing the value by its id.

Service Model:

@Entity
@Getter
@Setter
public class Service {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private BigDecimal price;
}

Service:

public class ServiceService {
    @Autowired
    private ServiceRepo serviceRepo;

    public Service getById(Long id) {
        return serviceRepo.findById(id).get();
    }
}

Repository:

public interface ServiceRepo extends JpaRepository<Service, Long>, JpaSpecificationExecutor<Service> {
    Optional<Service> findById(Long id);
}

And conroller:

@Controller
public class AllServices {
    @Autowired
    private ServiceRepo serviceRepo;

    @GetMapping("/services")
    public String allServices(Model model){
        List<Service> services = serviceRepo.findAll();
        model.addAttribute("services", services);
        return "services";
    }

    @Autowired
    private ServiceService serviceService;

    @GetMapping("/services/{id}")
    public ResponseEntity<Service> getServiceById(@RequestParam(value = "id", required = false) Long id){
        try{
            Service service = serviceService.getById(id);
            return new ResponseEntity<>(service, HttpStatus.OK);
        }catch(NoSuchElementException e){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
}

When it comes to all services, it returns all values, but here it doesn't see them.

enter image description here

enter image description here

enter image description here

These answers and many others didn't help:

JPA Repository.findById() returns null but the value is exist on db

Exception: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'params' is not present

nicole
  • 23
  • 3
  • You can try annotating your ```ServiceRepo``` with ```@Repository``` annotation and remove the ```findById(Long id)``` method declaration from this interface. Implementation for this method is already provided by ```SimpleJpaRepository```. So use the default one. – CyberMafia May 04 '23 at 16:22

2 Answers2

1

I see you're triying to use @RequestParam to get the param value, but you get the param like a @PathVariable, change the annotation to @PathVariable and try again.

I hope this link can help you.

https://www.baeldung.com/spring-pathvariable

Or if you want to use @RequestParam, so, send your request like: http://localhost:8080/service?id=1

  • Oh, thank you! Using http://localhost:8080/service?id=1 returns 404 Not Found, using @PathVariable and http://localhost:8080/services/1 still Cannot invoke \"coursework.MassageSalon.services.ServiceService.getById(java.lang.Long)\" because \"this.serviceService\" is null :_( – nicole May 03 '23 at 15:20
1

Service class need to be annotated as such. Otherwise the @Autowired will not work.

Solution

Add the annotation like this:

@Service
public class ServiceService {
// ...
}
Mar-Z
  • 2,660
  • 2
  • 4
  • 16