我正在尝试通过REST Web服务传递对象.以下是我的课程使用一些示例代码解释了我需要的功能.
Rest Web Service Class方法
@POST
@Path("/find")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({MediaType.APPLICATION_JSON})
public Response getDepartments(){
Response response = new Response();
try {
response.setCode(MessageCode.SUCCESS);
response.setMessage("Department Names");
Department dept = new Department("12", "Financial");
response.setPayload(dept);
} catch (Exception e) {
response.setCode(MessageCode.ERROR);
response.setMessage(e.getMessage());
e.printStackTrace();
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
响应类
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Response implements Serializable{
private static final long serialVersionUID = 1L;
public enum MessageCode {
SUCCESS, ERROR, UNKNOWN
}
private MessageCode code;
private String message;
private Object payload;
public MessageCode getCode() …
Run Code Online (Sandbox Code Playgroud) 我在从应用程序上下文中提取Spring bean时遇到问题.
当我尝试;
InnerThread instance = (InnerThread) SpringContextFactory.getApplicationContext().getBean("innerThread", InnerThread.class);
Run Code Online (Sandbox Code Playgroud)
我明白了
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'innerThread' must be of type [com.generic.InnerThread], but was actually of type [$Proxy26]
Run Code Online (Sandbox Code Playgroud)
如果没有getBean()调用中的指定类,我会得到一个ClassCastException(您可以在下面详细介绍).
InnerThread bean被初始化为非单例,因为我需要多个实例.InnerThread类还扩展了Thread.有趣的是,这个错误出现在OuterThread中,它的设置方式与InnerThread完全相同.
我试图在下面包含所有相关的代码清单/堆栈跟踪.拥有更多Spring经验的人能告诉我这里发生了什么吗?
OuterThread.java片段:
public class OuterThread extends Thread {
private Queue<InnerThread> createInnerThreads() {
Queue<InnerThread> threads = new ArrayBlockingQueue();
ApplicationContext ctx = SpringContextFactory.getApplicationContext();
int i = 0;
for (SearchRule search : searches) {
logger.debug("Number of times looped " + i++);
//Seprated lines to get a better sense of what is going on
Object …
Run Code Online (Sandbox Code Playgroud) 我试图在Java中调试涉及ClassCastException的问题.为了解决这个问题,我需要知道当我从Object转换为特定类型时会发生什么.任何人都可以向我解释Java转换运算符如何在Java级别和JVM级别上运行?
我有两个不同的webapps,每个都加载相同的A类和不同的类加载器.当我在会话中放入一个实例然后从另一个webapp获取它时,ClassCastException
会抛出一个.
例如,在webapp A中,我存储a
在会话中,然后在webapp B中,我a
从会话中获取并将其转换为A,ClassCastException
抛出.
有办法解决这个问题吗?
我遇到了一个非常奇怪的异常,我不知道如何找到原因.
商业背景:添加商品并同时列出价格表,商品有差价水平用户的5个价格.
在控制器中,首先使用dozer将goodForm转换为商品,然后调用goodsService来保存商品.在货物服务保存货物后,遍历货物价格表并填充货物ID到货物价格,
GoodsForm:
@Mapping("priceList")
List<GoodsPriceForm> goodsPriceFormList;
Goods:
List<GoodsPrice> priceList;
Controller:
Goods goods = BeanMapper.map(goodsForm, Goods.class);
goodsService.saveGoods(adminId, goods);
GoodsService:
goodsDao.save(goods);
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
goodsPriceDao.save(goods.getPriceList());
Run Code Online (Sandbox Code Playgroud)
但它抛出异常:
2015-11-27 17:10:57,042 [http-nio-8081-exec-8] ERROR o.a.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice] with root cause
java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice
at com.foo.goods.service.GoodsService$$Lambda$11/310447431.accept(Unknown Source) ~[na:na]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_51]
at com.foo.goods.service.GoodsService.saveGoods(GoodsService.java:34) ~[classes/:na]
Run Code Online (Sandbox Code Playgroud)
这个错误信息让我感到很困惑.另外我写一个单元测试想重复这个,但失败了.
GoodsForm form = new …
Run Code Online (Sandbox Code Playgroud) 我有两个ClassLoader加载相同的类.所以,显然这些不能相互投射.但是我需要访问在另一个ClassLoader中创建的对象.
我可以访问两个ClassLoader.如何在其他类中使用该对象?我不需要将对象转换为与当前ClassLoader匹配.
但问题是返回的对象的类型是Object
.所以,我必须抛弃该对象来访问某些方法.我怎样才能做到这一点?像下面这样的正常转换会导致ClassCastException,我已经知道了.
Mojo mojo = (Mojo) descriptor.getMojo();
Run Code Online (Sandbox Code Playgroud)
descriptor#getMojo()
返回一个类型的对象,Mojo
但该方法返回Object
.怎么办呢?
如果您需要进一步的信息,请告诉我.
我已经阅读了有关类加载的所有理论,但没有一个为此指定了适当的解决方案.
我的webapplication抛出以下ClassCastException:
java.lang.ClassCastException: org.postgresql.jdbc4.Jdbc4ResultSetMetaData
cannot be cast to org.postgresql.jdbc4.Jdbc4ResultSetMetaData
Run Code Online (Sandbox Code Playgroud)
显然这些类是相同的,所以这是一个奇怪的错误.
我发现使用-verbose:class
该类Jdbc4ResultSetMetaData
从两个jar文件加载,一个在我的Jetty lib/ext /目录中,另一个在我的webapplication中的/ WEB-INF/lib目录中.这些jar文件版本完全相同(md5sum完全相同)
这让我相信它确实是完全相同的类,但ClassCastException可能是由两个不同的类加载器加载同一个类引起的.
是否可以记录/跟踪使用jvm参数加载类的类加载器的名称或id?
我们有一个Map
带有Class对象的cache()TestClass
.另一个类加载器TestClass
再次在运行时初始化/加载,因此下面的代码会抛出一个ClassCastException
:
TestClass obj1 = (TestClass)map.get("key"); // throws a ClassCastException
Run Code Online (Sandbox Code Playgroud)
好吧,到目前为止,我确实理解这个问题.
所以,我试图找到背景信息为什么TestClass.class
不等于TestClass.class
.我假设不同的类加载器为ReferenceType设置了不同的id?有谁能向我解释背景?
我找到的最佳页面:http: //www.objectsource.com/j2eechapters/Ch21-ClassLoaders_and_J2EE.htm
以下是问题的简化版本:
SomeClass c = (SomeClass) obj.getSomeClassParent()
Run Code Online (Sandbox Code Playgroud)
并非总是如此,有时会触发异常
org.somepackage.SomeClass can't be cast to org.somepackage.SomeClass
Run Code Online (Sandbox Code Playgroud)
这怎么可能 ?我想这与JAI imageio是本机lib的事实有关,但是中继怎么会发生这种情况?我可能错过了什么,但是什么?
I'm using JAI imageio version 1.1
dcm4che 2.0.21 DICOM lib
Run Code Online (Sandbox Code Playgroud)
这是原始代码
ImageInputStream iis = ImageIO.createImageInputStream(src);
Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
ImageReader reader = iter.next();
DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();
Run Code Online (Sandbox Code Playgroud)
而原来的例外
org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam can't be cast to
org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam
Run Code Online (Sandbox Code Playgroud)
java java-native-interface casting classloader javax.imageio
我有两个类Server
(使用main方法,启动服务器)和StartPageServlet
Servlet.
代码中最重要的部分是:
public class Server {
public static void main(String[] args) throws Exception {
// some code
// I want to pass "anObject" to every Servlet.
Object anObject = new Object();
Server server = new Server(4000);
ServletContextHandler context =
new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addServlet(StartPageServlet.class, "/");
// more code
}
Run Code Online (Sandbox Code Playgroud)
和StartPageServlet:
public class StartPageServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Here I want to access "anObject"
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
这段代码使我感到恼火,有时它起作用,而另一些时候却不起作用!
NamedQuery :(名称=“ User.findByLogin”,查询=“从用户u在哪里选择u.login =:login”)
public User findByLogin(String login) {
Query query = em.createNamedQuery("User.findByLogin");
query.setParameter("login", login);
try {
return (User) query.getSingleResult();
} catch (javax.persistence.NoResultException ex) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
错误使我发疯!
Avertissement:EJB5184:在EJB UserFacade上调用期间发生系统异常,方法:public dz.admin.entity.User dz.admin.service.UserFacade.findByLogin(java.lang.String)Avertissement:javax.ejb.EJBException .. .. 由以下原因引起:java.lang.ClassCastException:dz.elit.admin.entity.User无法转换为dz.elit.admin.service.UserFacade.findByLogin(UserFacade.java: 45)