6

I'm writing REST service.

I want to get all records by date that I pass in @Path variable.

How Can I do that?

What I tried to do:

Model Class:

@Entity
@Table(name = "test")
public class Test {
@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String name;


    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDate beginDate;


    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDate endDate;


    private String activity;
}

Repository:

@Repository
public interface TestRepository  extends JpaRepository<Test, Integer> {

    List<Test> findAllByName(String name);

    List<Test> findAllByBeginDate(LocalDate date);
}

Service:

@Service
public class TestService {

@Autowired
private final TestRepository testRepository;

public TestService(TestRepository testRepository) {
    this.testRepository = testRepository;
}

public List<Test> getAllTestsByBeginDate(LocalDate date) {
    return  testRepository.findAllByBeginDate(date);
   }
 }  

Controller:

@RestController
@RequestMapping("/api/v1/")
public class TestController {

    @GetMapping("test/all/{date}")
    public List<Test> getAllTestsByBeginDate(@PathVariable ("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {


    return testService.getAllTestsByBeginDate(date);
    }
  }

When I pass date like this, I get errors:

Postman Get Method

kenny
  • 91
  • 1
  • 1
  • 4
  • There is more than one mapping for the corresponding url to map. For instance in the method `getAllTestsByName` date mapping could be mapped to String mapping , giving error – Rahul Agrawal Feb 24 '20 at 08:41
  • You need to show us the whole `TestController` class, or at least its `getAllTestsByName` method. – Amongalen Feb 24 '20 at 08:49

2 Answers2

10

This should work

@RestController
@RequestMapping("/api/v1/")
public class TestController {

    @GetMapping("test/all/{date}")
    public List<Test> getAllTestsByBeginDate(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {


    return testService.getAllTestsByBeginDate(date);
    }
  }

or this link will help

Swarit Agarwal
  • 2,520
  • 1
  • 26
  • 33
  • Do you have another method defined in TestController? – Swarit Agarwal Feb 24 '20 at 08:51
  • I used Request Param instead of RequestMapping and it works! But it looks not so prettiy as RequestMapping in url Thank you! – kenny Feb 24 '20 at 09:02
  • btw, what is best practice to pass such things as name and date in url? throughRequest Mapping or RequestParam? I think Request Mapping looks more pretty – kenny Feb 24 '20 at 09:06
  • In case direct searchById kind of moments I would go for PathParam, while in case I have multiple parameter(in a limit and expose of data isn't an issue) like name, age, I would prefer RequestParam. Now If parameters exceeds like more than 3, then POST body is always option. – Swarit Agarwal Feb 24 '20 at 09:15
  • Just to add on for RequestMapping. Usually RequestMapping is used to map URL to java defined method. – Swarit Agarwal Feb 24 '20 at 10:13
  • Because it confused me at first, when you pass a parameter to `@PathVariable`, here's what it needs to look like: `@PathVariable (value = "date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date` – chaotic3quilibrium Jan 26 '23 at 16:10
1

You can global configure any datetime format in application properties. Like:

spring.mvc.format.date=yyyy-MM-dd

spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss

spring.mvc.format.time=HH:mm:ss
Margi212
  • 143
  • 1
  • 10