解决“HTTP/1.1 405 Method not allowed”问题

      Apache、IIS、Nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否则会返回“HTTP/1.1 405 Method not allowed”错误。 即,将出错页面表单的method=“post”改为“get”即可...

发布了109 篇原创文章 · 获赞 5 · 访问量 8827
展开阅读全文

cxf restful 错误 HTTP 405 Method Not Allowed

05-12

初次使用cxf+restful 遇到错误 javax.ws.rs.ClientErrorException: HTTP 405 Method Not Allowed,有谁帮忙看看 测试的客户端和服务端的类: public class MyClient { public static void main(String[] args) throws Exception { //go("http://localhost:9000/ws/jaxrs/customer/1/info"); //go("http://localhost:9000/ws/jaxrs/customer/search?name=abc"); //go("http://localhost:9000/ws/jaxrs"); //go("http://localhost:9000/ws/jaxrs/customer/search?name=abc"); go1("http://localhost:9000/ws/jaxrs"); } private static void go(String url) throws Exception { /* String result = WebClient.create(url) .path("/sample/map") .query("name", "world") .get(String.class); System.out.println(result);*/ //room, Room.class RequestParam s = new RequestParam(); Response c=WebClient.create(url).path("/sample/map").post(s); System.out.println(c); }} --------------- public class MyServer { public static void main(String[] args) throws Exception { /*JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean(); factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); factoryBean.setResourceClasses(CustomerServiceImpl.class); factoryBean.setAddress("http://localhost:9000/ws/jaxrs"); factoryBean.create();*/ JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean(); factoryBean.setResourceClasses(ResponseService.class); factoryBean.setAddress("http://localhost:9000/ws/jaxrs"); factoryBean.create(); } } 用到的类:MapBean.class 、ResponseResult.class、ResponseResults.class、IResponseService.class、ResponseService.class @Path(value = "/sample") public class ResponseService implements IResponseService{ @Context private UriInfo uriInfo; @Context private Request request; @POST @Path("/map") @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public MapBean getMap(RequestParam requestParam) { System.out.println("####getMap#####"); System.out.println("Method:" + request.getMethod()); System.out.println("uri:" + uriInfo.getPath()); System.out.println(uriInfo.getPathParameters()); Map<String, ResponseResult> map = new HashMap<String, ResponseResult>(); List<ResponseResult> list = new ArrayList<ResponseResult>(); ResponseResult responseResult = null; for (int i = 0; i < 4;i ++) { responseResult = new ResponseResult(); responseResult = new ResponseResult(); responseResult.setId(i+""); responseResult.setName("JojO-" + i); map.put("key-" + i, responseResult); list.add(responseResult); } /*responseResult = new ResponseResult(); responseResult.setId(i); responseResult.setName("JojO-" + i);*/ MapBean bean = new MapBean(); bean.setMap(map); bean.setResponseResults(list); bean.setCode("code"); bean.setDesc("desc"); bean.setErrorCode("error"); bean.setErrorMsg("msg"); return bean; } } ---------------- @Path(value = "/sample") public interface IResponseService { @POST @Path("/map") @Consumes({"application/json","application/xml"}) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public MapBean getMap(RequestParam requestParam); } ------------------ @XmlRootElement(name="MapBean") public class MapBean { private String code; private String desc; private String errorCode; private String errorMsg; private Map<String, ResponseResult> map; private List<ResponseResult> responseResults; } -------------- @XmlRootElement(name="ResponseResults") public class ResponseResults { private List<ResponseResult> responseResults; } ------------- @XmlRootElement(name="ResponseResult") public class ResponseResult { private String id; private String name; } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览