最近研究rest,按照網上的demo搭了一個rest框架,但問題也出現了,請大牛們幫忙!
-------------------------
問題描述:
在同一個類里面,實作了兩個或多個get(這里的get也可以換成其他比如post)方法,但只能識別出一個get方法,其他的方法識別不出來。也就是在我書寫的demo里面,一個類中對每一個方式的請求只可以實作一個方法,多個get或者多個post,只可以識別其中的一個get或者post。
-------------------------
*************restlet-servlet.xml 配置內容*************
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 自動掃描(自動注入) -->
<context:component-scan base-package="cn.net.itsvr.android.*.service.*" />
<!-- 配置rest -->
<bean name="root" class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/student/{studentId}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="create" bean="StudentResource1" />
</bean>
</entry>
<entry key="/student/test/{studentId1}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="create" bean="StudentResource1" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="StudentResource1" class="org.lifeba.ws.resource.StudentResource" scope="prototype"/>
</beans>
*************web.xml 配置內容*************
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
<servlet>
<servlet-name>restlet</servlet-name>
<servlet-class>
org.restlet.ext.spring.RestletFrameworkServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<display-name>DEMO</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:resources/spring/applicationContext*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>InitServerContext</servlet-name>
<servlet-class>cn.net.sinodata.framework.servlet.InitServlet</servlet-class>
<init-param>
<param-name>serversInit</param-name>
<param-value>
cn.net.sinodata.example.init.PreLoadService
</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServerContext</servlet-name>
<url-pattern>/InitServerContext</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
************實作類***********
package org.lifeba.ws.resource;
import java.util.List;
import javax.ws.rs.Path;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.lifeba.ws.model.Student;
import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.net.sinodata.framework.util.PageUntils;
import cn.net.sinodata.webservice.service.CommTransService;
/***
*
* @author Steven
* http://www.lifeba.org
*/
public class StudentResource extends ServerResource{
private static Logger _log = Logger.getLogger(StudentResource.class);
private CommTransService wscommTransService;
public CommTransService getWscommTransService() {
return wscommTransService;
}
@Autowired
public void setWscommTransService(CommTransService wscommTransService) {
this.wscommTransService = wscommTransService;
}
/*private int id1;
private int id2;
private int id3;
private int id4;
private int id5;*/
@Override
protected void doInit() throws ResourceException {
/*id3 = Integer.valueOf((String) getRequestAttributes().get("studentId3"));
id4 = Integer.valueOf((String)( (getRequestAttributes().get("studentId4"))==null?9999:getRequestAttributes().get("studentId4")));
id5 = Integer.valueOf((String) getRequestAttributes().get("studentId5"));*/
}
@Get
@Path("/student/test")
public Representation get() {
String ab = (String) getRequestAttributes().get("studentId1");
String abc [] = ab.split("&");
//int id1 = Integer.valueOf((String)( (getRequestAttributes().get("p1"))==null?99:getRequestAttributes().get("p1")));
//int id2 = Integer.valueOf((String)( (getRequestAttributes().get("p2"))==null?99:getRequestAttributes().get("p1")));
/*_log.info("-------------------<><><><>get<><><>1<><><><><>-----------------------------"+id1);
_log.info("-------------------<><><><>get<><><>2<><><><><>-----------------------------"+id2);*/
/*_log.info("-------------------<><><><>get<><><>3<><><><><>-----------------------------"+id3);
_log.info("-------------------<><><><>get<><><>4<><><><><>-----------------------------"+id4);
_log.info("-------------------<><><><>get<><><>5<><><><><>-----------------------------"+id5);*/
Student student = ResourceHelper.findStudent(1);
JSONObject datajson = new JSONObject();
datajson.put("msg","success");
datajson.put("data",student);
return new StringRepresentation(changeJSONString(datajson));
}
@Delete
public Representation delete() {
int id1 = Integer.valueOf((String) getRequestAttributes().get("studentId1"));
PageUntils pageBean = new PageUntils();
_log.info("-------------------<><><><><><><><><><><><>-----------------------------");
String userid = "10055";
pageBean = wscommTransService.EnterApplyPage(userid);
_log.info("-------------------<><><><><><><><><><><><>-----------------------------");
int status = ResourceHelper.deleteStudent(id1);
return new StringRepresentation(String.valueOf(status));
}
@Put
public Representation put(Representation entity)
throws ResourceException {
int id1 = Integer.valueOf((String) getRequestAttributes().get("studentId1"));
Form form = new Form(entity);
Student student = ResourceHelper.findStudent(id1);
String name = form.getFirstValue("name");
int clsId = Integer.parseInt(form.getFirstValue("clsId"));
int sex = Integer.parseInt(form.getFirstValue("sex"));
int age = Integer.parseInt(form.getFirstValue("age"));
student.setClsId(clsId);
student.setName(name);
student.setSex(sex);
student.setAge(age);
return new StringRepresentation(String.valueOf(ResourceHelper.updateStudent(student)));
}
}
uj5u.com熱心網友回復:
有大神遇到過這種問題嗎 跪求解答啊uj5u.com熱心網友回復:
我覺得get你可以跟一個引數傳過去uj5u.com熱心網友回復:
謝謝轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/266547.html
標籤:應用服務器
