Java 如何读入REST请求的解组对象和原始XML?
我想将解组对象和原始XML都作为REST端点主要方法的参数 仅使用解组对象或原始XML进行操作似乎非常简单,但当它尝试创建REST端点时,我收到一个运行时异常 我所尝试的:Java 如何读入REST请求的解组对象和原始XML?,java,xml,rest,annotations,unmarshalling,Java,Xml,Rest,Annotations,Unmarshalling,我想将解组对象和原始XML都作为REST端点主要方法的参数 仅使用解组对象或原始XML进行操作似乎非常简单,但当它尝试创建REST端点时,我收到一个运行时异常 我所尝试的: @Path("/inbound") public class InboundWs extends SpringBeanAutowiringSupport { private final static Logger logger = APILogger.getLogger(InboundWs.class);
@Path("/inbound")
public class InboundWs extends SpringBeanAutowiringSupport {
private final static Logger logger = APILogger.getLogger(InboundWs.class);
@Autowired
private Service service;
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_PLAIN)
public Response inbound(Inbound request, String rawXml) {
logger.info("inbound() invloked on text: " + request.getText());
try {
service.inbound(
request.getSource(),
request.getDestination(),
request.getText());
} catch(BadRequestException e) {
return Response.status(Status.BAD_REQUEST).build();
} catch(Exception e){
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
return Response.status(Status.OK).build();
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Inbound", propOrder = {
"source",
"destination",
"text"
})
public class Inbound {
@XmlElement(required = true)
@XmlSchemaType(name = "source")
protected String source;
@XmlElement(required = true)
@XmlSchemaType(name = "destination")
protected String destination;
@XmlElement(required = true)
@XmlSchemaType(name = "text")
protected String text;
....
Feb 29, 2016 1:16:38 PM org.glassfish.jersey.internal.Errors logErrors
SEVERE: Following issues have been detected:
WARNING: Method public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String) on resource class abc.ws.InboundWs contains multiple parameters with no annotation. Unable to resolve the injection source.
Feb 29, 2016 1:16:38 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] Method public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String) on resource class abc.ws.InboundWs contains multiple parameters with no annotation. Unable to resolve the injection source.; source='ResourceMethod{httpMethod=POST, consumedTypes=[application/xml], producedTypes=[text/plain], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class abc.ws.InboundWs, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1e42af1]}, handlingMethod=public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String), parameters=[Parameter [type=class abc.xml.Inbound, source=null, defaultValue=null], Parameter [type=class java.lang.String, source=null, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:427)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:287)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
包含XML请求中所有字段的对象:
@Path("/inbound")
public class InboundWs extends SpringBeanAutowiringSupport {
private final static Logger logger = APILogger.getLogger(InboundWs.class);
@Autowired
private Service service;
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_PLAIN)
public Response inbound(Inbound request, String rawXml) {
logger.info("inbound() invloked on text: " + request.getText());
try {
service.inbound(
request.getSource(),
request.getDestination(),
request.getText());
} catch(BadRequestException e) {
return Response.status(Status.BAD_REQUEST).build();
} catch(Exception e){
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
return Response.status(Status.OK).build();
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Inbound", propOrder = {
"source",
"destination",
"text"
})
public class Inbound {
@XmlElement(required = true)
@XmlSchemaType(name = "source")
protected String source;
@XmlElement(required = true)
@XmlSchemaType(name = "destination")
protected String destination;
@XmlElement(required = true)
@XmlSchemaType(name = "text")
protected String text;
....
Feb 29, 2016 1:16:38 PM org.glassfish.jersey.internal.Errors logErrors
SEVERE: Following issues have been detected:
WARNING: Method public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String) on resource class abc.ws.InboundWs contains multiple parameters with no annotation. Unable to resolve the injection source.
Feb 29, 2016 1:16:38 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] Method public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String) on resource class abc.ws.InboundWs contains multiple parameters with no annotation. Unable to resolve the injection source.; source='ResourceMethod{httpMethod=POST, consumedTypes=[application/xml], producedTypes=[text/plain], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class abc.ws.InboundWs, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1e42af1]}, handlingMethod=public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String), parameters=[Parameter [type=class abc.xml.Inbound, source=null, defaultValue=null], Parameter [type=class java.lang.String, source=null, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:427)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:287)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
例外情况:
@Path("/inbound")
public class InboundWs extends SpringBeanAutowiringSupport {
private final static Logger logger = APILogger.getLogger(InboundWs.class);
@Autowired
private Service service;
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_PLAIN)
public Response inbound(Inbound request, String rawXml) {
logger.info("inbound() invloked on text: " + request.getText());
try {
service.inbound(
request.getSource(),
request.getDestination(),
request.getText());
} catch(BadRequestException e) {
return Response.status(Status.BAD_REQUEST).build();
} catch(Exception e){
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
return Response.status(Status.OK).build();
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Inbound", propOrder = {
"source",
"destination",
"text"
})
public class Inbound {
@XmlElement(required = true)
@XmlSchemaType(name = "source")
protected String source;
@XmlElement(required = true)
@XmlSchemaType(name = "destination")
protected String destination;
@XmlElement(required = true)
@XmlSchemaType(name = "text")
protected String text;
....
Feb 29, 2016 1:16:38 PM org.glassfish.jersey.internal.Errors logErrors
SEVERE: Following issues have been detected:
WARNING: Method public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String) on resource class abc.ws.InboundWs contains multiple parameters with no annotation. Unable to resolve the injection source.
Feb 29, 2016 1:16:38 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] Method public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String) on resource class abc.ws.InboundWs contains multiple parameters with no annotation. Unable to resolve the injection source.; source='ResourceMethod{httpMethod=POST, consumedTypes=[application/xml], producedTypes=[text/plain], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class abc.ws.InboundWs, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1e42af1]}, handlingMethod=public javax.ws.rs.core.Response abc.ws.InboundWs.inbound(abc.xml.Inbound,java.lang.String), parameters=[Parameter [type=class abc.xml.Inbound, source=null, defaultValue=null], Parameter [type=class java.lang.String, source=null, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:427)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:287)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)