服务请求类工作正常,服务请求助手也一样,但是userauthservice没有被命中! 程序退出时发现404未找到。
这是我的ServiceRequest类
public class ServiceFilter implements ContainerRequestFilter{
@Context
HttpServletRequest servletRequest;
@Override
public ContainerRequest filter(ContainerRequest request) {
System.out.println("request : "+request);
URI reqUri = request.getRequestUri();
System.out.println("req uri "+reqUri );
BufferedReader inFromClient = null;
try {
inFromClient = new BufferedReader(new InputStreamReader(
request.getEntityInputStream(), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String line;
StringBuffer buff = new StringBuffer();
try {
while ((line = inFromClient.readLine()) != null) {
buff.append(line);
}
inFromClient.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("###### Request parameters- " + buff);
String userName = null;
String password = null;
Long sessionId = null;
String serviceMethodName = request.getPathSegments()
.get(request.getPathSegments().size() - 1).getPath();
if (!NullObjects.isNull(reqUri)
&& !reqUri.toString().contains(
CommonConstants.REPORT_SERVICE_URI)) {
Response response = null;
try {
System.out.println("service method name : "+serviceMethodName);
ObjectMapper mapper = new ObjectMapper();
Map filterParams = mapper.readValue(buff.toString(),HashMap.class);
//call helper
ServiceRequestFilterHelper helper = new ServiceRequestFilterHelper();
request = helper.setRequestParams(request, filterParams, serviceMethodName);
} catch (Exception e) {
e.printStackTrace();
throw new WebApplicationException(response);
}
}
System.out.println("request"+request);
System.out.println("request URI: "+request.getRequestUri());
System.out.println("request-QueryParameters-"+request.getQueryParameters());
return request;
}
}
这是我的serviceRequestFilterHelper Class
public class ServiceRequestFilterHelper {
String userName = null;
String password = null;
public ContainerRequest setRequestParams(ContainerRequest request, Map reqParam, String serviceMethodName) {
if (serviceMethodName.equalsIgnoreCase(CommonConstants.SERVICE_METHOD_VALIDATE_CREDENTAILS)) {
request = setRequestParamValidateCreditials(request, reqParam);
}
return request;
}
这是我的UserService类
@Path("/userauthservice")
@PerSession
public class UserService implements Serializable{
@Path("/validateCredentails")
@POST
@Produces("application/json")
public Response validateCredentails(@QueryParam("user") String user,
@QueryParam("password") String password)
throws JSONException{
LoginDao loginDao = new LoginDao();
List<TAccessAuthorization> result = loginDao.list(user, password);
System.out.println("result : "+result.get(0).getAaCode());
return Response.status(200).entity(result).build();
}
}
这是我的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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>RESTfulWebServiceExample</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.arpit.javapostsforlearning.webservice</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.easyskool.filter.ServiceFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
这是我在XHR海报上看到的输出 POST on // localhost:8080 / test / rest / userauthservice / validateCredentails 状态:404未找到
输入值
网址: - // localhost:8080 / test / rest / userauthservice / validateCredentails
邮寄正文: - {“user”:“webadmin”,“password”:“webadmin”}
答案 0 :(得分:0)
XML文件中存在更新的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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>RESTfulWebServiceExample</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.easyskool.filter.ServiceFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>