Java注解配置rest服务_用spring注解可以发布rest服务吗

展开全部

使用spring注解是可以实现rest服务的,具体实现参考以下步骤代码:

1、配置32313133353236313431303231363533e4b893e5b19e31333363376466web.xml<?xml  version="1.0" encoding="UTF-8"?>

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

contextConfigLocation

/WEB-INF/rest-servlet.xml

org.springframework.web.context.ContextLoaderListener

rest

org.springframework.web.servlet.DispatcherServlet

2

rest

/

/index.jsp

2、配置rest-servlet.xml(这是spring的配置文件)

3、Controller代码实现package com.liqiu.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

@Controller

@RequestMapping("/simple")

public class SimpleController {

//映射路径/simple/index当访问这个路径时,执行这个方法

@RequestMapping("/index")

public String index(HttpServletRequest request ,HttpServletResponse response){

//response,request会自动传进来

request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!");

return "index.jsp";

}

//根据ID获取不同的内容,通过@PathVariable 获得属性

@RequestMapping(value="/{id}",method=RequestMethod.GET)

public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{

request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!
ID:"+id+"");

//response.getWriter().write("You put id is : "+id);

return "index.jsp";

//return null;

}

}

JSP页面

Spring3 RESTful

${message}

4、测试

也可以在页面输入不同的参数,获得不同的内容,输入地址:http://localhost:8080/SpringREST/simple/88888,这次执行的就是get方法,通过注解获取ID值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值