我在集成项目上工作了一段时间,主要是构建和集成服务。现在,我开始了一个自己的学习项目,从底层构建一个应用程序,目前我在编写web时遇到了困难。我使用了框架spring框架,我希望在第一阶段使用spring作为UI技术。不过,我打算使用REST控制器,因为我想公开一个API,该API可能会被移动客户端在我的下一步自我培训中使用。所以问题是:
如何将Web前端与RESTful控制器连接起来?
谢谢!
发布于 2013-05-30 08:46:07
您的REST控制器是mvc控制器,当然还有很多其他方法可以让它们返回json或xml。以下是两种选择:
轻而简单
正常地写控制器。让他们返回虚空。以HttpServletResponse作为in参数。使用json/xml序列化程序序列化结果并将输出写入响应。控制器不会转发到视图。
例如,您可以使用http://flexjson.sourceforge.net/来序列化为json。
举个例子:
@RequestMapping(value = "item/{someId}", method = RequestMethod.GET)
public void getItemById(HttpServletResponse response,
@PathVariable("someId") Long itemId) throws IOException {
... your code here, get the Item by the id ...
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
JSONSerializer serializer = new JSONSerializer();
serializer.exclude("*.class"); //reduce clutter in your output as you see fit
serializer.serialize(objectToSerialize, response.getWriter());
}当然,输出到json可以用其他方法存储。这种方法很容易实现,而且不难理解和使用。
更复杂
使用https://jersey.java.net/。这不是“自己动手”,而是一个完整的框架。遵循以下步骤:
XSD
这个XSD
<xsd:element name="customerBonus" type="customerBonus"/>
<xsd:complexType name="customerBonus">
<xsd:sequence>
<xsd:element name="bonusAmount" type="xsd:long"/>
<xsd:element name="bonusValidTo" type="xsd:date"/>
<xsd:element name="upcomingBonusAmount" type="xsd:long"/>
</xsd:sequence>
</xsd:complexType>变成这个java代码(为了简洁而不完整):
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = " CustomerBonus ",propOrder ={ "bonusAmount","bonusValidTo","upcomingBonusAmount“})公共类{
protected long bonusAmount;
@XmlElement(required = true)
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar bonusValidTo;
protected long upcomingBonusAmount;
/**
* Gets the value of the bonusAmount property.
*
*/
public long getBonusAmount() {
return bonusAmount;
}
/**
* Sets the value of the bonusAmount property.
*
*/
public void setBonusAmount(long value) {
this.bonusAmount = value;
}启用jersey servlet:
<servlet>
<servlet-name>Jersey Spring Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Spring Web Application</servlet-name>
<url-pattern>/yourstarturl/*</url-pattern>
</servlet-mapping>典型控制器:
@Path("cart") //base url for service
@Component
@Scope("request")
public class CartAndOrderResource extends AbstractResursResource {
@GET
@Produces({MediaType.APPLICATION_JSON}) //also xml possible here
@Path("{cartId}") //added to base url, final url = cart/{cartId}
public JAXBElement<Cart> getCart(@PathParam("cartId") String cartId) {
return new ObjectFactory().createCart(cartService.getCart(cartId));
}
}发布于 2013-05-30 04:31:13
关于如何做到这一点,有一个很好的教程这里。总之,您需要配置内置到Spring中的dispatcher servlet,以响应某些urls集,比如/webapp/*。这将导致所有以"/webapp/“开头的请求被转发到dispatcher servlet。
然后配置控制器类如下所示
@Controller
@RequestMapping("/users")
public class UsersController{
@RequestMapping("/users/{id}")
public String getUser(@PathVariable("id")String userId, Model model){
//get the user, etc etc etc
//return a string that points to the appropriate jsp page
}
}春天可以应付剩下的事。在本例中,您将用户id指定为URL的一部分。你也可以做各种各样的事情,比如处理帖子,获取等等。你可以创建任意数量的控制器和处理程序方法。在这种情况下,它将响应url“/webapp/user/someuserid”。
https://stackoverflow.com/questions/16827831
复制相似问题