use*_*201 4 java rest tomcat web-services jersey
我在linux发行版(Raspbian,Tomcat 7)上运行代码时遇到问题.该问题未出现在Windows/Tomcat 7/Eclipse下的测试环境中:
我的webservice只返回HTTP 204而没有别的.该日志不包含请求期间发生错误的任何迹象.它应该像在测试环境中那样用json响应.
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>***</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>**.useravailability.UserAvailabilityServletContextListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>REST Service</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>REST Service</servlet-name>
<servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
Run Code Online (Sandbox Code Playgroud)
usersService.xml
package **.webservice;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.google.gson.Gson;
import **.model.AccessManager;
@Path("/usersService")
public class UsersService {
@GET
@Produces("application/json")
public String getUser() {
String user = null;
ArrayList<String> userList = new ArrayList<String>();
try {
userList = new AccessManager().getUser();
Gson gson = new Gson();
user = gson.toJson(userList);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?为什么它在测试环境中有效?谢谢!
如果返回null,Jersey返回HTTP 204.
我认为这是最好的行为.
在您的情况下,您可能会引发异常,因此user为null.
如果要返回错误代码(可能更符合逻辑),可以替换
catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
通过
catch (Exception e) {
throw new WebApplicationException(404);
}
Run Code Online (Sandbox Code Playgroud)
如果你想说明找不到用户,或者你想要返回服务器端错误代码的5XX HTTp.
| 归档时间: |
|
| 查看次数: |
1956 次 |
| 最近记录: |