我是宁静的网络服务的新手。我是使用jersey在java中创建restful webservice的新手。这个web服务从mysql数据库获取数据,并应在xml中显示响应。 但是我总是从apache tomcat 7得到响应错误 500 。在控制台中没有显示错误或异常,除了println方法正在将传递给它的字符串diplaying ..但是服务器正在给出 500 错误..请帮帮我
userData.java
package com.userdb;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class userData {
public String name;
public int iduser;
public userData(){}
public userData(String name, int iduser) {
this.name = name;
this.iduser = iduser;
}
public String getName() {
return name;
}
public int getIduser() {
return iduser;
}
public void setName(String name) {
this.name = name;
}
public void setIduser(int iduser) {
this.iduser = iduser;
}
}
airtime.java
package com.userdb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/resttest")
public class airtime {
ResultSet rs=null;
String msg="hello";
Connection con=null;
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/get_users")
public List<userData> get_users(){
List<userData> retUser=new ArrayList<>();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
System.out.println("DriveManager");
PreparedStatement ps=con.prepareStatement("SELECT * FROM users");
rs=ps.executeQuery();
System.out.println(rs);
while(rs.next()){
userData obj=new userData();
obj.setIduser(rs.getInt("iduser"));
obj.setName(rs.getString("name"));
retUser.add(obj);
System.out.println("userData obj added to list");
}
con.close();
} catch (Exception e){
e.printStackTrace();
}
return retUser;
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>userdb</display-name>
<servlet>
<servlet-name>Jersey WebService</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.userdb</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey WebService</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
解决方案1:
调整userData.java
的顶部以在文件顶部包含以下行:
package com.userdb;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class userData
{...}
说明:您会注意到已添加了XmlAccessorType注释和XMLAccessType。在对象中使用setter时,需要此配置。您会注意到调整代码以不使用setter(上面排除了)也允许您在浏览器中查看RESTful服务。
解决方案2:
更快的替代方法是将公共变量name
和iduser
设置为私有。这也可以避免在映射到xml时发生冲突。
答案 1 :(得分:0)
应该有一个警告,例如&#34; ...找不到媒体类型= application / xml ...&#34; ...
可能的原因可能是您没有包含此依赖项。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.21.1</version>
</dependency>
这是我可行的pom依赖
<dependencies>
<!-- jersey dependency -->
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.21.1</version>
</dependency>
<!-- make jersey auto marshel to json -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.21.1</version>
</dependency>
<!-- make jersey auto marshel to xml -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.21.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>stax2-api</artifactId>
<version>3.1.4</version>
</dependency>
</dependencies>
可行的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Rest</display-name>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.blithe.resource</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.scanning.recursive</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
可行的资源
package com.blithe.resource;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.blithe.model.User;
@Path("helloworld")
public class HelloWorldResource
{
@GET
@Produces(MediaType.APPLICATION_XML)
public List<User> getStringArray(){
List<User> list = new ArrayList<>();
User u = new User();
u.setName("testName");
list.add(u);
return list;
}
}
和模型
package com.blithe.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
希望这会对你有所帮助。
顺便说一句,最好不要在pom.xml中包含太多可能重复的依赖项,因为它们中的一些可能会相互冲突。它将花费大量时间进行调试。因此,最好找出真正需要的依赖项。