cat tag


Spring 2.0 + Jersey rest 기반 tutorial Study

기본적으로 스프링 프레임워크 버전을 2.0이라는 전제 하에 동작하는 코드이다.
사실 3.0 이상의 스프링 프레임워크라면 큰 의미가 없다.  2.5인가? restful 기반의 코드들이 추가된게...

* pom.xml
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>1.8</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.8</version>
</dependency>
이 세가지만 dependency로 관리해주면 되긴 하지만, 굳이 반드시 필요하지 않은 일부 라이브러리들도 있는 듯? 사용하지 않으면 exclude시켜주면 된다.

* 필수 라이브러리
  • asm-3.1.jar

  • jackson-core-asl-1.7.1.jar

  • jackson-jaxrs-1.7.1.jar

  • jackson-mapper-asl-1.7.1.jar

  • jackson-xc-1.7.1.jar

  • jaxb-api-2.2.2.jar

  • jaxb-impl-2.2.3-1.jar

  • jersey-core-1.8.jar

  • jersey-json-1.8.jar

  • jersey-server-1.8.jar

  • jersey-spring-1.8.jar

  • jettison-1.1.jar
라이브러리만 추가해서 사용할 경우 위의 라이브러리들을 추가하면 된다.

* web.xml
<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            com.mypackage
        </param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
스프링 프레임워크와 연동해서 개발하는 것이 아니면 굳이 com.sun.jersey.spi.spring.container.servlet.SpringServlet를 서블릿으로 작성할 필요가 없고, com.sun.jersey.spi.container.servlet.ServletContainer를 서블릿으로 작성하면 되는데, 스프링 프레임워크를 사용하면서 SpringServlet를 사용하지 않으면 객체를 정상적으로 DI시켜주지 않는다. 


* 일종의 버그 같은데 resource로 활용하는 클래스가 하나도 없으면 아래와 같은 에러가 발생한다. 클래스 앞에 @Path를 달아놓으면 아래와 같은 에러가 발생하지 않고, 리소스를 가져올 수 있다.

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1298)
at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:169)
at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:771)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)


* resource 클래스

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.core.InjectParam;

@Path("/login")
public class LoginResource {

@InjectParam
private DataService service;
@GET
@Path("/{name}")
@Produces(value = MediaType.APPLICATION_JSON)
public User getUser(@PathParam("name") String name) {
return service.getUserByName(name);
}
}

@InjectParam어노테이션을 작성하면 해당 객체를 주입시켜주는데, 이 어노테이션을 사용하지 않고 @Inject를 사용하면 연결되어 있는 다른 객체들이 정상적으로 주입되지 않는 경우가 있다.



* 참고로 리턴 타입이 한글이거나 영어가 아닌 문자가 있을 경우 MediaType.APPLICATION_JSON만 @Produces의 값으로 지정하면 문자가 깨질 수 있으므로 MediaType.APPLICATION_JSON + ";charset=utf-8"로 작성하면 된다.

덧글

  • 궁금 2015/12/04 05:10 # 삭제 답글

    전자정부 3.1에서는 안되는건가요?
  • 궁금 2015/12/04 05:10 # 삭제 답글

    전자정부 3.1에서는 안되는건가요?
  • 궁금 2015/12/04 05:10 # 삭제 답글

    전자정부 3.1에서는 안되는건가요?
  • 궁금 2015/12/04 05:10 # 삭제 답글

    전자정부 3.1에서는 안되는건가요?
  • 궁금 2015/12/04 05:12 # 삭제 답글

    전자정부 2.6에서는 되나요????

    javax.servlet.ServletException: Loading the class com.sun.jersey.spi.container.servlet.ServletContainer failed.
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:85)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.getInstance(ContainerManagedInstanceReloader.java:56)
    at jeus.servlet.loader.ServletReloader.getServlet(ServletReloader.java:44)
    at jeus.servlet.engine.ServletWrapper.executeServlet(ServletWrapper.java:158)
    at jeus.servlet.engine.ServletWrapper.execute(ServletWrapper.java:149)
    at jeus.servlet.engine.RequestProcessor.run(RequestProcessor.java:201)
    at jeus.util.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:1211)
    at jeus.util.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:1261)
    at jeus.servlet.engine.WebThreadPoolExecutor$WebRequestWorker.run(WebThreadPoolExecutor.java:340)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
    at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:79)
    at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:89)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:700)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:678)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    at javax.servlet.GenericServlet.init(GenericServlet.java:242)
    at jeus.servlet.loader.ServletReloader.doInitInstance(ServletReloader.java:63)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:75)
    ... 9 more
    Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at jeus.server.classloader.RootClassLoader.findClass(RootClassLoader.java:68)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)


    이런 에러가 뜨는데 혹시 아시나요?
  • 궁금 2015/12/04 05:12 # 삭제 답글

    전자정부 2.6에서는 되나요????

    javax.servlet.ServletException: Loading the class com.sun.jersey.spi.container.servlet.ServletContainer failed.
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:85)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.getInstance(ContainerManagedInstanceReloader.java:56)
    at jeus.servlet.loader.ServletReloader.getServlet(ServletReloader.java:44)
    at jeus.servlet.engine.ServletWrapper.executeServlet(ServletWrapper.java:158)
    at jeus.servlet.engine.ServletWrapper.execute(ServletWrapper.java:149)
    at jeus.servlet.engine.RequestProcessor.run(RequestProcessor.java:201)
    at jeus.util.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:1211)
    at jeus.util.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:1261)
    at jeus.servlet.engine.WebThreadPoolExecutor$WebRequestWorker.run(WebThreadPoolExecutor.java:340)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
    at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:79)
    at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:89)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:700)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:678)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    at javax.servlet.GenericServlet.init(GenericServlet.java:242)
    at jeus.servlet.loader.ServletReloader.doInitInstance(ServletReloader.java:63)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:75)
    ... 9 more
    Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at jeus.server.classloader.RootClassLoader.findClass(RootClassLoader.java:68)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)


    이런 에러가 뜨는데 혹시 아시나요?
  • 궁금 2015/12/04 05:12 # 삭제 답글

    전자정부 2.6에서는 되나요????

    javax.servlet.ServletException: Loading the class com.sun.jersey.spi.container.servlet.ServletContainer failed.
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:85)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.getInstance(ContainerManagedInstanceReloader.java:56)
    at jeus.servlet.loader.ServletReloader.getServlet(ServletReloader.java:44)
    at jeus.servlet.engine.ServletWrapper.executeServlet(ServletWrapper.java:158)
    at jeus.servlet.engine.ServletWrapper.execute(ServletWrapper.java:149)
    at jeus.servlet.engine.RequestProcessor.run(RequestProcessor.java:201)
    at jeus.util.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:1211)
    at jeus.util.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:1261)
    at jeus.servlet.engine.WebThreadPoolExecutor$WebRequestWorker.run(WebThreadPoolExecutor.java:340)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
    at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:79)
    at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:89)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:700)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:678)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    at javax.servlet.GenericServlet.init(GenericServlet.java:242)
    at jeus.servlet.loader.ServletReloader.doInitInstance(ServletReloader.java:63)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:75)
    ... 9 more
    Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at jeus.server.classloader.RootClassLoader.findClass(RootClassLoader.java:68)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)


    이런 에러가 뜨는데 혹시 아시나요?
  • 궁금 2015/12/04 05:12 # 삭제 답글

    전자정부 2.6에서는 되나요????

    javax.servlet.ServletException: Loading the class com.sun.jersey.spi.container.servlet.ServletContainer failed.
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:85)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.getInstance(ContainerManagedInstanceReloader.java:56)
    at jeus.servlet.loader.ServletReloader.getServlet(ServletReloader.java:44)
    at jeus.servlet.engine.ServletWrapper.executeServlet(ServletWrapper.java:158)
    at jeus.servlet.engine.ServletWrapper.execute(ServletWrapper.java:149)
    at jeus.servlet.engine.RequestProcessor.run(RequestProcessor.java:201)
    at jeus.util.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:1211)
    at jeus.util.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:1261)
    at jeus.servlet.engine.WebThreadPoolExecutor$WebRequestWorker.run(WebThreadPoolExecutor.java:340)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
    at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:79)
    at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:89)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:700)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:678)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    at javax.servlet.GenericServlet.init(GenericServlet.java:242)
    at jeus.servlet.loader.ServletReloader.doInitInstance(ServletReloader.java:63)
    at jeus.servlet.loader.ContainerManagedInstanceReloader.checkAndInitInstance(ContainerManagedInstanceReloader.java:75)
    ... 9 more
    Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at jeus.server.classloader.RootClassLoader.findClass(RootClassLoader.java:68)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)


    이런 에러가 뜨는데 혹시 아시나요?
댓글 입력 영역