https://github.com/akgithubtest/WhatIsItBroken
मैं समझ नहीं पा रहा हूं कि जेएक्सबी उपयोग का एक उदाहरण त्रुटि class Common.Broken nor any of its super class is known to this context.
क्यों फेंक रहा है और दूसरा पूरी तरह से ठीक क्यों काम करता है। मैंने प्रत्येक को एक दृश्य अंतर में तोड़ दिया है, और अंतर को भी अलग नहीं कर सकता। मुझे यहां क्या समझ नहीं आ रहा है?
Main.java में, मेरे पास है। यह एक साधारण http सर्वर है जो दो मार्गों पर सुनता है: working
और broken
। broken
काम नहीं करता। working
करता है।
import Common.Broken;
import Common.Working;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
public class Main {
public static void main(String[] args) throws Exception {
int port = 8888;
HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 0);
httpServer.createContext("/broken", new BrokenHandler());
httpServer.createContext("/working", new WorkingHandler());
httpServer.setExecutor(null);
httpServer.start();
}
static class BrokenHandler implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange)
throws IOException
{
try {
Broken broken = new Broken();
broken.age = 100;
broken.id = 3;
broken.name = "This doesn't work. Why?";
ResponseHandler responseHandler = new ResponseHandler(broken);
responseHandler.RespondBroken(httpExchange);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
static class WorkingHandler implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange)
throws IOException
{
try {
Working working = new Working();
working.age = 100;
working.id = 3;
working.name = "This works";
ResponseHandler responseHandler = new ResponseHandler(working);
responseHandler.RespondWorking(httpExchange);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}
Common/Broken.java
में मेरे पास निम्नलिखित है (जो Common/Working.java
के समान है, लेकिन वर्ग और फ़ाइल का नाम Broken
कहा जाता है।
package Common;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement (name = "broken")
public class Broken {
@XmlElement public String name;
@XmlElement public int age;
@XmlAttribute public int id;
}
केवल इसके लिए (यदि मुझसे कुछ छूट गया है, तो यह है Common/Working.java
:
package Common;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement (name = "working")
public class Working {
@XmlElement public String name;
@XmlElement public int age;
@XmlAttribute public int id;
}
अंत में, मेरे पास एक प्रतिक्रिया हैंडलर है। दोनों कार्य लगभग समान हैं।
import Common.Working;
import Common.Broken;
import com.sun.net.httpserver.HttpExchange;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.IOException;
import java.io.StringWriter;
import java.io.OutputStream;
public class ResponseHandler {
public Broken broken;
public Working working;
private JAXBContext jaxbContext;
private Marshaller marshaller;
public ResponseHandler(Working working)
throws JAXBException
{
this.working = working;
this.Init();
}
public ResponseHandler(Broken broken)
throws JAXBException
{
this.broken = broken;
this.Init();
}
private void Init()
throws JAXBException {
this.jaxbContext = JAXBContext.newInstance(Working.class);
this.marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
}
public void RespondWorking(HttpExchange httpExchange)
throws JAXBException, IOException
{
java.io.StringWriter output = new StringWriter();
this.marshaller.marshal(this.working, output);
String response = output.toString();
httpExchange.sendResponseHeaders(200, response.length());
OutputStream outputStream = httpExchange.getResponseBody();
outputStream.write(response.getBytes());
outputStream.close();
}
public void RespondBroken(HttpExchange httpExchange)
throws JAXBException, IOException
{
java.io.StringWriter output = new StringWriter();
this.marshaller.marshal(this.broken, output);
String response = output.toString();
httpExchange.sendResponseHeaders(200, response.length());
OutputStream outputStream = httpExchange.getResponseBody();
outputStream.write(response.getBytes());
outputStream.close();
}
}
मैं यहां जो खो रहा हूं उसे नहीं देख सकता। क्या मैं पागल हो रहा हूँ? (जीथब लिंक में स्रोत)।
1 उत्तर
यह इस तथ्य से संबंधित है कि आप Init()
दोनों कंस्ट्रक्टर के लिए कॉल कर रहे हैं जो Working
लेता है और कंस्ट्रक्टर जो Broken
लेता है।
Init()
विधि के अंदर आप JAXBContext.newInstance(Working.class);
को कॉल करते हैं। जब Init()
विधि को कंस्ट्रक्टर के माध्यम से Broken
के साथ कॉल किया जाता है, तो JAXBContext.newInstance(Broken.class);
कहा जाता है, जो ऐसा नहीं है, और इसीलिए यह उस त्रुटि को फेंक रहा है।
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।