我正在尝试使用jersey和maven从api获取数据。但是出现以下错误:
javax.servlet.ServletException: A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MusicService,parent=MusicController,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1830564791)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.ashwin.lisnepal.controller.MusicController errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.ashwin.lisnepal.controller.MusicController
我的控制器类是:
package com.ashwin.lisnepal.controller;
import com.ashwin.lisnepal.response.ResponseGen;
import com.ashwin.lisnepal.service.MusicService;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/hello")
public class MusicController {
@Inject
private MusicService musicService;
@GET
@Path("getAllMusic")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllMusic() {
return ResponseGen.success(musicService.getAllMusic());
}
}
我的服务类别是:
package com.ashwin.lisnepal.service;
import com.ashwin.lisnepal.dao.MusicDao;
import com.ashwin.lisnepal.model.Album;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.engine.control.CompositeCacheManager;
import org.apache.log4j.Logger;
import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
*
* @author lis
*/
@Stateless
public class MusicService {
@Inject
private MusicDao musicDao;
private JCS cache;
public List<Album> getAllMusic() {
// First, try to get the music list from the cache...
List albumList = (List) cache.get("musicCache");
// If the userList does not exist in the cache, build it
// from the repository request and stick it in the cache.
if(albumList == null) {
new MusicDao();
return musicDao.getAllTopHundredMusic();
/* cache.put("activeUserList",userList);*/
}
return albumList;
}
}
我的道课是:
package com.ashwin.lisnepal.dao;
import com.ashwin.lisnepal.model.Album;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.engine.control.CompositeCacheManager;
import org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class MusicDao {
private JCS cache;
static final
List<Album> albumList=new ArrayList<Album>() {{
albumList.add(new Album(1, "Toby Mac", "Diverse City"));
albumList.add(new Album(2, "Toby Mac", "Diverse City"));
albumList.add(new Album(3, "Toby Mac", "Diverse City"));
albumList.add(new Album(4, "Toby Mac", "Diverse City"));
albumList.add(new Album(5, "Toby Mac", "Diverse City"));
albumList.add(new Album(6, "Toby Mac", "Diverse City"));
albumList.add(new Album(7, "Toby Mac", "Diverse City"));
albumList.add(new Album(8, "Toby Mac", "Diverse City"));
albumList.add(new Album(9, "Toby Mac", "Diverse City"));
albumList.add(new Album(10, "Toby Mac", "Diverse City"));
albumList.add(new Album(11, "Toby Mac", "Diverse City"));
albumList.add(new Album(12, "Toby Mac", "Diverse City"));
albumList.add(new Album(13, "Toby Mac", "Diverse City"));
albumList.add(new Album(14, "Toby Mac", "Diverse City"));
albumList.add(new Album(15, "Toby Mac", "Diverse City"));
albumList.add(new Album(16, "Toby Mac", "Diverse City"));
albumList.add(new Album(17, "Toby Mac", "Diverse City"));
albumList.add(new Album(18, "Toby Mac", "Diverse City"));
albumList.add(new Album(19, "Toby Mac", "Diverse City"));
}};
public MusicDao(){
try {
System.out.println("Going to put in cache........................");
CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
Properties props = new Properties();
props.put("jcs.default","DC");
props.put("jcs.default.cacheattributes",
"org.apache.jcs.engine.CompositeCacheAttributes");
// lots more props.put - this is basically the contents of cache.ccf
Logger log
= Logger.getLogger(MusicDao.class);
ccm.configure(props);
// Load the cache
cache = JCS.getInstance("musicCache");
// Initialize the cache
/* allAlbumList.forEach(s-> {
cache.put(new Integer(1),
s)
});*/
int i=0;
for(Album a:albumList){
cache.put(new Integer(i+1), a);
}
} catch (CacheException e) {
System.out.println("here");
e.printStackTrace();
}
}
public List<Album> getAllMusic() {
return null;
}
public List<Album> getAllTopHundredMusic() {
return albumList;
}
}
我的pom.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ashwin</groupId>
<artifactId>ApplicationCache</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ApplicationCache Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jcs/jcs -->
<dependency>
<groupId>jcs</groupId>
<artifactId>jcs</artifactId>
<version>1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/concurrent/concurrent -->
<dependency>
<groupId>concurrent</groupId>
<artifactId>concurrent</artifactId>
<version>1.3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>ApplicationCache</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我试图访问URL http://localhost:8080/hello/getAllMusic
,但出现错误。当我尝试打印普通的普通“ Hello World”时,它正在打印,但现在却让我出错:
java.lang.IllegalArgumentException:尝试解决com.ashwin.lisnepal.controller.MusicController错误的依赖性时