![](/img/trans.png)
[英]Spring Boot maven project from Eclipse doesn't work in Intellij
[英]My function doesn't work in a spring boot project when it is working on a java project
@Override
public Song nextSong(Long playlistId, Long songId) {
String sql = "SELECT s.id, s.genre, s.artist_name, s.track_name, s.duration_ms, s.popularity "
+ "FROM T_SONGPLAYLIST AS sp INNER JOIN T_SONGS AS s "
+ "ON s.id = sp.song_id "
+ "WHERE sp.playlist_id = ? ";
List<Song> songs = new ArrayList();
songs = jdbcTemplate.query(sql,rowMapperSong,playlistId);
return gettingIt(songs, songId);
这是 PlayerRepositoryJDBCImpl.java 文件中的方法
public Song gettingIt(List<Song> songs, Long songId) {
DoublyLinkedList d = new DoublyLinkedList(songs);
d.display();
int index = -1;
for(int i = 0; i<songs.size();i++)
{
if(songId == d.takeNodebyIndex(i).getdata().getId()) {
System.out.println(d.takeNodebyIndex(i).getdata().getId());
index = i+1;
}
}
return d.takeNodebyIndex(index).getdata();
}
}
我正在使用此映射发送获取请求:
@RequestMapping(method=RequestMethod.GET, value ="/nextSong/{playlistId}-{songId}")
当我使用 nextSong 方法向链接的 url 发送获取请求时,它返回给我这些 output:
[2m2021-05-28 16:47:45.364[0;39m [32m INFO[0;39m [35m14892[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2021-05-28 16:47:45.365[0;39m [32m INFO[0;39m [35m14892[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mc.d.musicplayer.MusicPlayerApplication [0;39m [2m:[0;39m Started MusicPlayerApplication in 2.187 seconds (JVM running for 403.063)
[2m2021-05-28 16:47:45.368[0;39m [32m INFO[0;39m [35m14892[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36m.ConditionEvaluationDeltaLoggingListener[0;39m [2m:[0;39m Condition evaluation unchanged
[2m2021-05-28 16:47:46.504[0;39m [32m INFO[0;39m [35m14892[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring FrameworkServlet 'dispatcherServlet'
[2m2021-05-28 16:47:46.504[0;39m [32m INFO[0;39m [35m14892[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization started
[2m2021-05-28 16:47:46.508[0;39m [32m INFO[0;39m [35m14892[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization completed in 4 ms
[Song [id=154, genre=Dance, artist_name=2NE1, track_name=COME BACK HOME, duration_ms=229627, popularity=57] Song [id=524, genre=Electronic, artist_name=Flux Pavilion, track_name=I Can t Stop, duration_ms=305786, popularity=56] Song [id=1514, genre=Reggae, artist_name=Don Carlos, track_name=English Women, duration_ms=182933, popularity=32] ]
[Song [id=154, genre=Dance, artist_name=2NE1, track_name=COME BACK HOME, duration_ms=229627, popularity=57] Song [id=524, genre=Electronic, artist_name=Flux Pavilion, track_name=I Can t Stop, duration_ms=305786, popularity=56] Song [id=1514, genre=Reggae, artist_name=Don Carlos, track_name=English Women, duration_ms=182933, popularity=32] ]
这是双向链表方法:
package com.dataStructuresProject.musicplayer.model;
import java.util.List;
public class DoublyLinkedList {
private Node head;
private Node tail;
private int length;
public DoublyLinkedList(List<Song> arr) {
this.head = null;
this.tail = null;
this.length = 0;
for (int i=0;i<arr.size();i++)
this.add(arr.get(i));
}
public int getLength() {
return length;
}
public void add(Song data)
{
Node temp = new Node(data);
if(head == null)
head = temp;
else
{
Node iter = head;
while (iter.next != null)
iter = iter.next;
iter.next = temp;
temp.prev = iter;
tail = temp;
}
this.length++;
}
public void display()
{
Node iter = head;
System.out.print("[");
while(iter != null)
{
System.out.print(iter.data+"\t");
iter = iter.next;
}
System.out.println("]");
}
public void reverseDisplay()
{
Node iter = tail;
System.out.print("[");
while(iter != null)
{
System.out.print(iter.data+"\t");
iter = iter.prev;
}
System.out.println("]");
}
public Node takeNodebyIndex(int i)
{
Node iter = head;
int iteratorIndex;
iteratorIndex = 0;
while(iteratorIndex++<i)
iter = iter.next;
return iter;
}
public class Node {
Song data;
private Node next;
private Node prev;
public Node(Song data)
{
this.data = data;
this.next = null;
this.prev = null;
}
public Node() {
this.next = null;
this.prev = null;
}
public Node(Node prev, Node next) {
this.next = next;
this.prev = prev;
}
public Song getdata() {
return this.data;
}
}
}
最后,如果我使用 http://localhost:8080/api/nextSong/2-154 url 发送 get 请求,该方法必须返回 id 为 524 的歌曲,但它总是返回双向链表的第一首歌曲.
因为您没有中断条件:
for(int i = 0; i<songs.size();i++)
{
if(songId == d.takeNodebyIndex(i).getdata().getId()) {
System.out.println(d.takeNodebyIndex(i).getdata().getId());
index = i+1;
break; // this should be added here
}
}
实际上,我通过在比较它们时将双链表中的 songId 和 id 都转换为字符串变量来解决问题。 我不知道是什么导致了这个问题,但是转换字符串对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.