저는 JAX-RS를 처음 사용하고 Embedded Tomcat을 사용하는 데 어려움을 겪고 있습니다. 아래Jersey와 Jackson을 사용하여 JSON으로 모델을 반환하려고 할 때 내부 서버 오류가 발생했습니다.
- 코드는 작동 : http://yourhost.me:8080/rest/resource/hello
- 하지만 난은 "내부 서버 오류"를 얻을 : http://yourhost.me:8080/rest/resource/car
누구나/자동차 URI가 작동하려면 내가 부족을 확인? JSON으로 응답을 얻으려고합니다. 또한 spring-hateoas를 사용하여 링크를 얻으려면 아래에서 어떻게 업데이트 할 수 있는지 아는 사람이 있습니까? Tomcat이 아닌 비 Jersey 프로젝트에서 스프링 - hateoas 링크가 작동하지만이 설정으로 작동시키지 못합니다. 톰캣에 대한
Gradle을 프로젝트
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/libs-release" }
}
dependencies {
compile "org.apache.tomcat.embed:tomcat-embed-core:7.0.34"
compile "org.apache.tomcat.embed:tomcat-embed-logging-juli:7.0.34"
compile "org.apache.tomcat.embed:tomcat-embed-jasper:7.0.34"
compile "org.apache.tomcat:tomcat-jasper:7.0.34"
compile "org.apache.tomcat:tomcat-jasper-el:7.0.34"
compile "org.apache.tomcat:tomcat-jsp-api:7.0.34"
compile 'javax.ws.rs:javax.ws.rs-api:2.0-m02'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.12'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'com.fasterxml.jackson.core:jackson-databind'
compile 'org.springframework.hateoas:spring-hateoas:0.16.0.RELEASE'
compile 'org.springframework:spring-web:4.0.3.RELEASE'
compile 'com.sun.jersey:jersey-json:1.7'
}
의 web.xml은 Main.java는
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>REST Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.sample.resources.ResourceLoader</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
을
-package com.sample.main;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception {
String webappDirLocation = "src/main/webapp/";
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
Context context = tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
File configFile = new File(webappDirLocation + "WEB-INF/web.xml");
context.setConfigFile(configFile.toURI().toURL());
tomcat.start();
tomcat.getServer().await();
}
}
ResourceLoader.java 독립 Tomcat을 실행 - 난 곳 여기 것은 리소스 등록 중
package com.sample.resources;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class ResourceLoader extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resource
classes.add(HelloWorldRestResource.class);
return classes;
}
}
HelloWorldRestResource.java - 각 URI를 처리 할 수있는 컨트롤러/안녕하세요/자동차
package com.sample.resources;
import com.sample.model.Car;
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("/resource")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldRestResource {
@GET
@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello World from Tomcat Embedded with Jersey!";
}
@GET
@Path("/car")
@Produces(MediaType.APPLICATION_JSON)
public Response showModel() {
Car mine = new Car(2014);
return Response.ok().entity(mine).build();
}
}
Car.java - 그것은 JSON
package com.sample.model;
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.hateoas.ResourceSupport;
@XmlRootElement()
public class Car extends ResourceSupport {
private int year;
public Car(int year) {
this.year = year;
}
}