gpt4 book ai didi

json - JAX-RS JAXB JSON 响应不起作用(MessageBodyProviderNotFoundException)

转载 作者:行者123 更新时间:2023-12-01 01:04:47 28 4
gpt4 key购买 nike

我一直在研究如何创建 JAX Restful 服务……使用此处提供的指南 - Jersey

如第 2.3.2 节所述,我在 Maven 中添加了以下依赖项 -

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.0</version>
</dependency>

在 web.xml 中
<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.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.hms.rs.controller.MyApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

和 MyApp.java
public class MyApp extends Application{

@Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>() {{
// Add your resources.
System.out.println("From the Myapp...");
add(Patient.class);
add(PatientController.class);

// Add LoggingFilter.
add(LoggingFilter.class);
}};
}
}

患者.java -
@XmlRootElement(name = "Patient")
public class Patient {

private String patientFName;
private String patientLName;
private int patientAge;
private String patientSex;
private String patientParentSpouse;
private String patientQual;
private String patientOccupation;
private String patientComments;

public Patient()
{
}

Setters and Getters....

}

患者 Controller .java -
@Path("/ManagePatient")
public class PatientController {
@GET
@Path("/getPatient")
@Produces(MediaType.APPLICATION_XML)
public Patient printPatient() {
System.out.println("Hello.... from the PatientController");
Patient ptnt = new Patient();
ptnt.setPatientFName("FirstN");
ptnt.setPatientLName("LName");
ptnt.setPatientAge(30);
ptnt.setPatientSex("M");
ptnt.setPatientParentSpouse("ParentSpuse");
ptnt.setPatientQual("engg");
ptnt.setPatientOccupation("software");
ptnt.setPatientComments("comments here");

System.out.println("Patient = " + ptnt);

//return ptnt.toString();
return ptnt;
}

当我尝试通过浏览器访问它时 @ localhost:8080/HMS_Web/services/ManagePatient/getPatient

我正进入(状态
javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class com.hms.app.ui.beans.Patient, genericType=class com.hms.app.ui.beans.Patient.

我还在日志中看到以下警告-
WARNING: A provider com.hms.app.ui.beans.Patient registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.hms.app.ui.beans.Patient will be ignored. 

如果 Jersey 2.0 支持基于 JAXB 的 xml 或 json 支持,如 Jersey 指南中@“8.1.1.2. 基于 JAXB 的 JSON 支持”部分所述,我不确定为什么我会收到 Provider 错误。

任何 JAX-WS 专家都可以帮助我理解并为我提供有关如何解决这种情况的指导吗?

先感谢您

最佳答案

您正在通过浏览器访问该服务,因此您的 PatientController 将尝试将响应呈现为 html,我想这就是

javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class com.hms.app.ui.beans.Patient, genericType=class com.hms.app.ui.beans.Patient.

尝试通过 jersey 客户端 api 使用服务,如下所示:
WebTarget webTarget = client.target("http://localhost:8080/HMS_Web/services/ManagePatient/getPatient");

Patient patient = webTarget.request(MediaType.APPLICATION_XML_TYPE).get(Patient.class);

对于警告:
WARNING: A provider com.hms.app.ui.beans.Patient registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.hms.app.ui.beans.Patient will be ignored. 

我认为你应该删除:
add(Patient.class);

在您的 我的应用 . Patient 只是一个 POJO,它既不是资源也不是提供者。

关于json - JAX-RS JAXB JSON 响应不起作用(MessageBodyProviderNotFoundException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18048429/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com