Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

java - Spring Boot JPA : GetRequest gives me no output

Hi guys, My GetRequest gives me a 500, and I don't know why. I am trying to make a JPQL Query to select Artist name and get the names of them with the getArtistbySomething The SQL Select statement is working in MySQL

#My Repository:

public interface ArtistRepository extends JpaRepository<Artist, Long> {
    @Query("SELECT a.name, a.id, count(p.artist) FROM Artist a, Production p, Clip c WHERE p.artist = a.id AND p.clip = c.id AND c.size > 5000 and p.date > ?1 GROUP BY p.artist order by count(p.artist) desc ")
    public List<Artist> getArtistBySomething(long date);

}

My Restcontroller with the getRequest:

import ch.zhaw.springboot.entities.Artist;
import ch.zhaw.springboot.repositories.ArtistRepository;
import ch.zhaw.springboot.repositories.ClipRepository;
import ch.zhaw.springboot.repositories.ProductionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@CrossOrigin
public class ArtistRestController {

    @Autowired
    private ArtistRepository artistRepository;
    @Autowired
    private ClipRepository clipRepository;
    @Autowired
    private ProductionRepository productionRepository;


    @GetMapping(value = "universal2/artist")
    public ResponseEntity<List<Artist>> getArtist() {
        List<Artist> result = this.artistRepository.findAll();

        if (!result.isEmpty()) {
            return new ResponseEntity<List<Artist>>(result, HttpStatus.OK);
        } else {
            return new ResponseEntity<List<Artist>>(HttpStatus.NOT_FOUND);
        }
    }


    
   @GetMapping(value = "universal2/artist/{date}")
    public ResponseEntity<List<Artist>> getArtistBySomething(@PathVariable("date") long date) {
        List<Artist> result = this.artistRepository.getArtistBySomething(date);

        if (!result.isEmpty()) {
            return new ResponseEntity<List<Artist>>(result, HttpStatus.OK);
        } else {
            return new ResponseEntity<List<Artist>>(HttpStatus.NOT_FOUND);
        }
    }}

The getArtistBySomething Output in JSON with URL: http://localhost:8080/universal2/artist/1516233600

"timestamp": "2021-01-07T11:57:46.250+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/universal2/artist/100': {public org.springframework.http.ResponseEntity ch.zhaw.springboot.restcontroller.ArtistRestController.getArtistById(long), public org.springframework.http.ResponseEntity ch.zhaw.springboot.restcontroller.ArtistRestController.getArtistBySomething(long)}",
"path": "/universal2/artist/1516233600"

}

I would be very grateful for any advice!

Kind regards!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have mapped getArtist() and getArtistBySomething(@PathVariable("date") long date) to the same endpoint universal2/artist. To fix it you can add something like this the second one @GetMapping(value = "universal2/artist/date/{date}")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...