我开始学习休息,我尝试做REST以返回Aluno的JSON,当我在“postman”或“firefox”上测试时,我收到这个404错误,但访问jsp并不是没有问题。
我尝试了许多想法,我改变了休息的版本 - 休眠,尝试做另一个休息,但只有相同的答案。
使用的技术:Wildfly,RestEasy,EasyCriteria,JPA,Hibernate。
网址:http://localhost:8080/teachmanager/ws/aluno
怎么了?!
答案:
<html>
<head>
<title>Error</title>
</head>
<body>Not Found</body>
</html>
休息:
@Path("aluno")
public class AlunoRest {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String busca() {
Dao<Aluno> dao = new Dao<Aluno>(JPAUtil.getEntityManager(), Aluno.class);
Aluno aluno = dao.getById(1);
return aluno.toJson();
}
}
的pom.xml:
<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>br.com.ilearning.teachmanager</groupId>
<artifactId>teachmanager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.13.Final</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>uaihebert.com</groupId>
<artifactId>EasyCriteria</artifactId>
<version>3.0.0</version>
</dependency>
<!-- <dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.4.6.Final</version>
</dependency> -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>2.2.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.5.0.Final</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
的web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/ws</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>restEasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>restEasy</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
我将网页模块版本更改为3.0,那就可以了!但我不明白为什么不在3.1上工作!
我发现了一种新的方法来执行该Web服务,而不使用web.xml。
我创建了一个配置类:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("service")
public class RestManager extends Application{
}
并创建一个正常的休息类:
@Path("teste")
public class Teste {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getTeste() {
return "teste";
}
@GET
@Path("lista")
@Produces(MediaType.APPLICATION_JSON)
public String lista() {
String ret = "";
try {
Dao<Aluno> dao = new Dao<Aluno>(JPAUtil.getEntityManager(), Aluno.class);
List<Aluno> lista = dao.getAll();
ret = new Gson().toJson(lista);
} catch (Exception e) {
ret = new Gson().toJson(e.getMessage());
}
return ret;
}
}