I'm trying to create my first RESTFull web service application using java. I have postgres, netbeans 14, jdk-18.0.2 an tomcat 10 installed. I have created a maven java web application and some RESTfull web services from the database (option NEW -> RestFull web services from the database).
Netbeans created several classes for me like the following:
@Stateless
@Path("task")
public class TaskFacadeREST extends AbstractFacade<Task> {
@PersistenceContext(unitName = "com.mycorp_testproject_war_1.0-SNAPSHOTPU")
private EntityManager em;
public TaskFacadeREST() {
super(Task.class);
}
@POST
@Override
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void create(Task entity) {
super.create(entity);
}
Entity
@Table(name = "TASK")
@NamedQueries({
@NamedQuery(name = "Task.findAll", query = "SELECT t FROM Task t"),
@NamedQuery(name = "Task.findById", query = "SELECT t FROM Task t WHERE t.id = :id"),
@NamedQuery(name = "Task.findByProjectId", query = "SELECT t FROM Task t WHERE t.projectId = :projectId"),
@NamedQuery(name = "Task.findByTypeId", query = "SELECT t FROM Task t WHERE t.typeId = :typeId")})
public class Task implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Long id;
And Application Config class:
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.mycorp.testproject.service.LinkTypeFacadeREST.class);
resources.add(com.mycorp.testproject.service.ProjectFacadeREST.class);
resources.add(com.mycorp.testproject.service.TaskFacadeREST.class);
resources.add(com.mycorp.testproject.service.TaskLinkFacadeREST.class);
resources.add(com.mycorp.testproject.service.TaskTypeFacadeREST.class);
}
}
Then there were some errors in project path, dependencies, building process, pom.xml and plugin versions that i managed to fix. Now my project can be successfully deployed and shows "Hellow world" index page. But rest service endpoints still return 404 error.
What i have tried to do myself:
<servlet>
<display-name>Task RESTfull web service</display-name>
<servlet-name>TaskService</servlet-name>
<servlet-class>com.mycorp.testproject.service.TaskFacadeREST</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mycorp.testproject.service.ApplicationConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>TaskService</servlet-name>
<url-pattern>/webresources/task</url-pattern>
</servlet-mapping>
The first option didn't help a bit, the second returned error that TaskFacadeREST class is not a servlet class.
Now i'm completely stuck, can't find any tutorial on deploying JAX-RS endpoints to Tomcat and don't know which way to go. Will be gratefull for any info, documentattion or tutorial links on what to do next as netbeans tutorials are based on glassfish and do not contain any info on such problems.
Tomcat 10 implements APIs from Jakarta EE 9 onwards. You appear to be using the Java EE 8 APIs. There is a major package change between Java EE 8 and Jakarta EE 9 (javax.* -> jakarta.*).
Deploy on Tomcat 9 rather than on Tomcat 10 and things should improve.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.