I have recently changes our app from Java 1.7 and Tomcat 7 to Java 1.8 and Tomcat 8.
The issue i am facing is that when one servlet call another the request parameter is now an ApplicationHttpRequest instead of the HttpServletRequest as it was before. This should not be an issue but for some different behaviour for the same methods.
For example request.getParameter("param1") will return a null for the HttpServletRequest if the parameter is not in the request.
But for ApplicationHttpRequest it with throw an exaction like so; java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;
If the parameter does exist in the request then they behave the same.
I have written a utility method to handle the issue. But i was wondering if there is an configuration change i can do to get the original HttpServletRequest back?
public static String getRequestParameter(HttpServletRequest request, String key) {
if (request == null || StringUtils.isBlank(key)) return null;
try {
return request.getParameter(key);
} catch (ClassCastException e1) {
}
return null;
}
ApplicationHttpRequest comes with Tomcat from Apache and is a wrapper which extends HttpServletRequestWrapper. Try changing the dynamic web module version to an earlier version.
It is also possible that you have got your IDE to automatically import the wrong classes.
Here is a full list of classes which should be imported to ensure that HTTPServletRequest is used:
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
Related
I am very new to groovy script runner and I have been trying to delete a field in JIRA but I always get NullPointerException.
I do understand what NullPointerMeans and I have read multiple posts online and on atlassian but nothing has helped.
Any idea / help is appreciated on how I can achieve this
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField_toDelete = customFieldManager.getCustomFieldObject("12345")
customFieldManager.removeCustomField(customField_toDelete);
Stacktrace
java.lang.NullPointerException
at com.atlassian.jira.issue.managers.DefaultCustomFieldManager.removeCustomField(DefaultCustomFieldManager.java:490)
at com.atlassian.jira.issue.CustomFieldManager$removeCustomField$7.call(Unknown Source)
at Script2441.run(Script2441.groovy:20)
According to the JIRA documentation
customFieldManager.getCustomFieldObject("12345")
Will return null if there is no customer field in JIRA with the identifier "12345", hence why
customFieldManager.removeCustomField(customField_toDelete)
Throws a null pointer (because customField_toDelete is null)
If the method removeCustomField was #Nullable (i.e was annotated with the Nullable annotation) then it would be ok to pass a null to the method, but since it is not annotated with the method, it throws an exception.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have imported project to my new machine (to eclipse) and it threw 88 errors even that on previous machine it was working good. here are the types of errors:
MINUTES cannot be resolved or is not a field
Syntax error
Method not applicable to arguments
isEmpty() undefined
And when I check it all of syntax is good etc.
Anyone had similar problem?
Here's code which throws an exception (MINUTES cannot be resolved or is not a field )
try {
TimeUnit.MINUTES.sleep(Config.DOCS_CHECK_INTERVAL);
} catch (InterruptedException e) {
log.error("Exception", e);
}
Imports:
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.security.cert.X509Certificate;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.ikubasienki.core.Config;
All of your errors suggest that you are using an outdated Java Version. For example, in Java 5, there was no TimeUnit.MINUTES: https://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/TimeUnit.html. It has been added in Java 6.
Assumed that you are calling isEmpty() on a String Object, the same is true: String.isEmpty() has been added in Java 6: https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#isEmpty(), hence you would get an isEmpty() undefined error when building the project with Java 5.
Please check the settings of the "JRE System Library" you are using to build the project in Eclipse:
Verify that the path shown for the jar files matches the java runtime you want to use.
If that still does not help, create a new Project in Eclipse with a simple class like
package com.example;
public class Simple {
public static void main(String[] args) {
System.err.println(System.getProperty("java.version"));
System.err.println("".isEmpty());
}
}
and run it. The output should be something like
1.7.0_80
true
Another common source of errors is the Compiler Compliance Level setting of your project:
However, this would not explain the "undefined symbol" errors since it affects the language level only (e.g. whether enum or generics are allowed). It might explain your syntax error, though.
Make sure your custom jre in eclipse points to jre folder (if you're using jde make sure twice it's not ponting to jde but to jre inside jde!!)!
I have a problem producing JSON in my application.
I'm trying a tutorial about Consuming Java Restful Web Service with AngularJS.
I've created a dynamic web project as my RESTful server and then added the following libraries:
asm-3.3.1.jar
jackson-core-asl-1.9.9.jar
jackson-jaxrs-1.9.9.jar
jackson-mapper-asl-1.9.9.jar
jackson-xc-1.9.9.jar
jersey-bundle-1.8.jar
jersey-bundle-1.9.jar
jersey-client-1.9.jar
jersey-core-1.9.jar
jersey-json-1.9.jar
jersey-media-json-jettison-2.4.jar
jersey-server-1.9.1.jar
jersey-servlet-1.12.jar
jettison-1.3.3.jar
jsr311-api-1.1.1.jar
Here is the service :
package ws;
import java.awt.PageAttributes.MediaType;
import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import entities.Prospect;
#Path("prospect")
public class ProspectRestful {
#GET
#Path("findall")
#Produces(MediaType.APPLICATION_JSON)
public List<Prospect> findAll(){
List<Prospect> result = new ArrayList<Prospect>();
result.add(new Prospect(35, "Name 35", "last35"));
result.add(new Prospect(36, "Name 36", "last36"));
return result;
}
}
But I'm getting this error:
APPLICATION_JSON cannot be resolved or is not a field.
I've seen in another question that jersey-media-json-jettison-2.4.jar should be there so I added it but no sign.
I'm sure I'm not using a Maven project, in the same tutorial they didn't use Maven either.
Remove this
import java.awt.PageAttributes.MediaType;
and add this
import javax.ws.rs.core.MediaType;
In my case it is not working, so if anyone facing the error then try...
import org.springframework.http.MediaType;
It worked for me...
I wrote an HttpServlet and I named it "Stick" and then defined a Class named "John" in it.
In addition, I wrote another HttpServlet and named it "StickDetails".
Both of the Servlets are in the same package.
I want StickDetails servlet to function as a Main function (I want to use the "Stick" class inside it).
The problem starts when I try to write the command in StickDetails "John j = new John;"
but john isen't recognized! (""String cannot resolved to a type"")
what did I do wrong here?
Here you see StickDetails servlet:
package wood;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StickDetails extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException,ServletException {
Stick a; // Stick is not recognized "String cannot resolved to a type"
}
}
I am using Eclipse IDE for java Developers
You Need To define a new class John, don't put it Stick Servlet.
In your Stick Servlet you can create instance of John Class(if required) and after your logic, set attributes which are required in StickDetails Servlet and then use RequestDispatcher in Stick Servlet to forward request to StickDetails.
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchContext;
import com.f2s.bean.Recentbean;
#Path("search")
public class SearchResource {
private List<Recentbean> ra;
#Path("book")
#GET
public List<Recentbean> findBooks(#Context SearchContext searchContext)
{
SearchCondition<Recentbean> condition = searchContext.getCondition(Recentbean.class);
return condition.findAll(ra);
}
error :- annotated with GET of resource, class com.f2s.restws.SearchResource, is not recognized as valid resource method.
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
First of all, you "question" is actually not a question. You're just presenting an error that you get. No explanation, no expected behaviour, no question asked. Providing some more details would be helpful.
Secondly, have you had a look on the other questions dealing with the presented exception? Here there are a few:
Exception :com.sun.jersey.spi.inject.Errors$ErrorMessagesException
Jersey: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
Errors$ErrorMessagesException when using Jersey in Java
Because of my reputation I couldn't add this as a comment...