I am using jar files instead of maven because i dont have internet.
problem: i am using jersey implemenation of jax-rs. i need to convert the java to json but it is giving the following error
java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.PropertiesHelper.getValue(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;
at org.glassfish.jersey.moxy.json.MoxyJsonFeature.configure(MoxyJsonFeature.java:67)
at org.glassfish.jersey.model.internal.CommonConfig.configureFeatures(CommonConfig.java:730)
at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:648)
at org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:829)
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:453)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1236)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4910)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5192)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1387)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
the code i am using is:
package mypack;
import javax.websocket.server.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.server.BackgroundScheduler;
import entity.Res;
#Path("/s")
public class webapi {
#GET
#Path("q/{name}")
#Produces(MediaType.APPLICATION_JSON)
public Res method(#PathParam("name") String name){
try{
Res r = new Res();
r.setId(5);
r.setName(name);
return r;
}catch(Exception e){
System.out.println(e.getMessage());
return null;
}
}
}
the res class is as follow:
package entity;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Res {
#XmlElement int id;
#XmlElement String name;
public Res() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
the jar files that i am using are:
web.xml file is :
<?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">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>mypack</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
what is the reason for the error?
I Think there may be a problem with the jar Versioning . Because the jar that you used for your Project is Older Version. Please update your lib to update the jersey dependency as i see and predict the error.
I would suggest you to add jars atleast of Version 2.7 instead of 2.2 or 2.3 as your dependency Says.
Please Update your lib Folder then Clean your Project .
Build Your Project again. After Updating your lib with New jar Files .
Thank You
Related
I have created a REST webservice 'JSONService' ( using Jersey implementation JAX-RS) with a POST method and created 'JerseyClientPost'.
What I am trying to achieve is to send a POST from my JerseyClientPost. Getting all values into a 'player' object and sending along with response back. But while trying to send the POST request from the postman, it throws
HTTP Status 404 - Not Found, The requested resource is not available.
Any pointers to address this problem will be much appreciated ?
Request Url : http://localhost:8080/WeekendSoccer/test/register/create
Following are my environment details:
JDK 1.7.0_79,
Tomcat Server v7.0,
Jersey (jaxrs-ri-2.25.1),
Web.xml
//REST Resource is given below
package webService;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import dto.RegisterPlayer;
#Path("/register")
public class JSONService {
#POST
#Path("/create")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public Response createPlayerInJSON(RegisterPlayer player) {
String result = "Player Created : " +player;
return Response.status(201).entity(result).build();
}
}
//Below is the Jersey Client Post
package dao;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class JerseyClientPost {
public static void main(String[] args) {
try {
RegisterPlayer player = new RegisterPlayer();
player.setName("Link");
player.setEmail("link#test.com");
player.setCompany("Test Ltd");
Client client = ClientBuilder.newClient(new ClientConfig().register( LoggingFilter.class ));
WebTarget webTarget
= client.target("http://localhost:8080/WeekendSoccer/test");
WebTarget playerWebTarget
= webTarget.path("/register/create");
Invocation.Builder invocationBuilder
= playerWebTarget.request(MediaType.APPLICATION_JSON);
Response response
= invocationBuilder
.post(Entity.entity(player,MediaType.APPLICATION_JSON));
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
// RegisterPlayer model class with getters, setters
public class RegisterPlayer implements Serializable{
private String name;
private String email;
private String company;
public RegisterPlayer()
{
}
public RegisterPlayer(String name, String email, String company)
{
super();
this.name = name;
this.email = email;
this.company = company;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
....
}
// Web.xml
<?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">
<display-name>WeekendSoccer</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Register Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>webService</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Register Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>JSON Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>dao</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSON Service</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
</web-app>
//Listed all jars
aopalliance-repackaged-2.5.0-b32.jar
gson-2.2.2.jar
hk2-api-2.5.0-b32.jar
hk2-locator-2.5.0-b32.jar
hk2-utils-2.5.0-b32.jar
jackson-annotations-2.3.2.jar
jackson-core-2.3.2.jar
jackson-databind-2.3.2.jar
jackson-jaxrs-base-2.3.2.jar
jackson-jaxrs-json-provider-2.3.2.jar
jackson-module-jaxb-annotations-2.3.2.jar
javassist-3.20.0-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.5.0-b32.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-entity-filtering-2.17.jar
jersey-guava-2.25.1.jar
jersey-media-jaxb.jar
jersey-media-json-jackson-2.17.jar
jersey-server.jar
mysql-connector-java-5.1.40-bin.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar
Firstly, I have never tested your client side code but I guess your problem is about server side and tested in Postman. you should add #XmlRootElement annotation in your pojo class RegisterPlayer like that:
#XmlRootElement
public class RegisterPlayer implements Serializable{
private String name;
private String email;
private String company;
public RegisterPlayer()
{
}
public RegisterPlayer(String name, String email, String company)
{
super();
this.name = name;
this.email = email;
this.company = company;
}
and added these dependencies in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
If you already have these jars it is fine. It works for me right now. I hope it helps to you.
jackson-core-2.9.3.jar
jackson-mapper-asl-1.9.13.jar
jackson-core-asl-1.9.13.jar
asm-3.3.1.jar
jersey-bundle-1.19.4.jar
jsr311-api-1.1.1.jar
json-20170516.jar
jersey-server-1.19.4.jar
jersey-core-1.19.4.jar
jersey-multipart-1.19.4.jar
mimepull-1.9.3.jar
jackson-annotations-2.7.0.jar
jackson-databind-2.7.0.jar
I'm still not sure, why do you have following mapping in web.xml ? As theren't any servlet class associated with this servlet name. You should removed them
<servlet-mapping>
<servlet-name>JSON Service</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
I see you have following entries and they do have servlet class associated.
<servlet-mapping>
<servlet-name>Register Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
That means, you target url should be
http://localhost:8080/WeekendSoccer/rest/register/create
instead of
http://localhost:8080/WeekendSoccer/test/register/create
my web.xml file :
Web.xml
I am trying to do a simple jersey restful webservice hello world but its not working. Can anyone check where I am doing wrong?
I am using the below URL :
http://localhost:8080/learning1/rest/firstRest/User1
<?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">
<servlet>
<servlet-name>Learn</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.java.learning1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Learn</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
My restfulwebservice java file : java file
Server is responding with HTTP Status code 404 : not found
package org.java.learning1;
import javax.websocket.server.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
#Path("/firstRest")
public class firstRest {
#GET
#Produces("MediaType.TEXT_HTML")
#Path("{name}")
public String sendResponse(#Context HttpHeaders httpHeaders, #PathParam("name") String name){
String greeting = "hello";
return greeting;
}
}
Hello user7481861 and welcome to stackoverflow!
You have two errors in your code, I've corrected them bellow and added a comment in each, saying what was wrong.
package org.java.learning1;
//import javax.websocket.server.PathParam; <-- incorrect import
import javax.ws.rs.PathParam; // <<-- correct import
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
#Path("/firstRest")
public class firstRest {
#GET
#Produces(MediaType.TEXT_HTML) // No quotes like stdunbar said in the comments
#Path("/{name}") // missing slash before name
public String sendResponse(#Context HttpHeaders httpHeaders, #PathParam("name") String name){
String greeting = "hello " + name; // concatenate the string with the variable name
return greeting;
}
}
I am following up this tutorial
https://www.javacodegeeks.com/2014/03/building-java-web-application-using-hibernate-with-spring.html
I am having this error >>>> the requested resource could not be found
web.xml file content
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>studentHibernateServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>studentHibernateServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/jpaContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>Archetype Created Web Application</display-name>
</web-app>
The controller content
package com.github.elizabetht.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.github.elizabetht.model.Student;
import com.github.elizabetht.model.StudentLogin;
import com.github.elizabetht.service.StudentService;
#Controller
#SessionAttributes("student")
public class StudentController {
#Autowired
private StudentService studentService;
#RequestMapping(value="/signup", method=RequestMethod.GET)
public String signup(Model model) {
Student student = new Student();
model.addAttribute("student", student);
return "signup";
}
#RequestMapping(value="/signup", method=RequestMethod.POST)
public String signup(#Valid #ModelAttribute("student") Student student, BindingResult result, Model model) {
if(result.hasErrors()) {
return "signup";
} else if(studentService.findByUserName(student.getUserName())) {
model.addAttribute("message", "User Name exists. Try another user name");
return "signup";
} else {
studentService.save(student);
model.addAttribute("message", "Saved student details");
return "redirect:login.html";
}
}
#RequestMapping(value="/login", method=RequestMethod.GET)
public String login(Model model) {
StudentLogin studentLogin = new StudentLogin();
model.addAttribute("studentLogin", studentLogin);
return "login";
}
#RequestMapping(value="/login", method=RequestMethod.POST)
public String login(#Valid #ModelAttribute("studentLogin") StudentLogin studentLogin, BindingResult result) {
if (result.hasErrors()) {
return "login";
} else {
boolean found = studentService.findByLogin(studentLogin.getUserName(), studentLogin.getPassword());
if (found) {
return "success";
} else {
return "failure";
}
}
}
}
Please assist me I a beginner
Your dispatcher servlet only listens to *.html. You can either call a url having this as its suffix, i.e.:
http://localhost::8080/[YourAppName]/[filename].html
Or you can change the servlet mapping to something more generic:
<servlet-mapping>
<servlet-name>studentHibernateServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
and call i.e.:
http://localhost::8080/[YourAppName]/signup
Update
Your servlet version is set to 2.5 which is very old. Its 3.1 at the moment. You might want to update it, to at least 3.0 for Tomcat 7, for example. Therefore the tutorial is a little outdated too, search for Spring 4 MVC to find modern ones.
I met a surious problem when I want to develop an API to call an "exterior" method (this method is called "createClientNode" which is a simple request for neo4j).
Anyways, this is my web.xml :
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>API</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>API</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>/API/*</url-pattern>
</servlet-mapping>
</web-app>
And this my class :
package API;
import javax.ws.rs.DefaultValue;
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 org.neo4j.graphdb.Node;
import function.query.weceipt.GraphManagement;
#Path("/API")
public class MyService {
#POST
#Path("/node")
#Produces({ MediaType.APPLICATION_JSON })
public Node createNode(#DefaultValue("default_id") #QueryParam("ID") String id) {
GraphManagement gm = new GraphManagement();
Node node = gm.createClientNode(id);
return node;
}
#GET
#Path("/node/{ID}")
#Produces({ MediaType.APPLICATION_JSON })
public Node getNode(#PathParam("ID") String id) {
GraphManagement gm = new GraphManagement();
Node node = gm.getClientNode(id);
return node;
}
#Path("/test")
#GET
#Produces(MediaType.TEXT_PLAIN)
public void afficher() {
System.out.println("test");
}
}
the error that I get is "the Servlet jersey-serlvet is not available".
I have the impression that I have-perhaps- a problem with a specific JAR or Tomcat, but I coudn't find the cause of this problem. So, can anyone help me please because I tried a loooong time with this!
i'm new to REST Service. i've done a small REST Webservice application and deployed in my online tomcat webserver,
My link -->
http://sample.com.au/REST/WebService/MyMethod?name=sss
but i'm getting the following message
The requested URL /REST/WebService/MyMethod was not found on this server.
when i run the similar in eclipse locally its working....
can anyone please tell me some solution for this...
my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
:
:
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
</web-app>
My FeedService.java
package webService;
import java.util.ArrayList;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import model.ProjectManager;
import com.google.gson.Gson;
import dto.FeedObjects;
#Path("/WebService")
public class FeedService {
#GET
#Path("/MyMethod")
#Produces("application/json")
public String names(#QueryParam("name") String name)
{
System.out.println("name----------->"+name);
String feeds = null;
try
{
ArrayList<FeedObjects> feedData = null;
ProjectManager projectManager= new ProjectManager();
feedData = projectManager.GetFeeds(name);
Gson gson = new Gson();
System.out.println(gson.toJson(feedData));
feeds = gson.toJson(feedData);
}
catch (Exception e)
{
System.out.println("Exception Error"); //Console
}
return feeds;
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/iloadlogistics.com.au"/>
Refer here. Specifically check Example 4.1 and Example 4.5. Should you not deploy that way?