Cannot make a REST call with a URL other than / *

I am trying to make a simple REST web application using Tomcat 7, Apache Wink and Jackson JSON Processor, but it seems to hit the wall. If I look in the web.xml file, I see:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Example Web Application</display-name> <servlet> <servlet-name>ExampleServlet</servlet-name> <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.dummy.example.server.ExampleApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ExampleServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app> 

Now, if I instead of / URL instead of the URL character, the REST call works, but when I use / services / *, it fails.

In my ExampleApplication application, I see:

 package com.dummy.example.server; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider; import org.codehaus.jackson.map.AnnotationIntrospector; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; public class ExampleApplication extends Application { /** * Get the list of service classes provided by this JAX-RS application */ @Override public Set<Class<?>> getClasses() { Set<Class<?>> serviceClasses = new HashSet<Class<?>>(); serviceClasses.add(com.dummy.example.server.services.Employee.class); return serviceClasses; } @SuppressWarnings("deprecation") @Override public Set<Object> getSingletons() { Set<Object> s = new HashSet<Object>(); // Register the Jackson provider for JSON // Make (de)serializer use a subset of JAXB and (afterwards) Jackson annotations // See http://wiki.fasterxml.com/JacksonJAXBAnnotations for more information ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector primary = new JaxbAnnotationIntrospector(); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); mapper.getDeserializationConfig().setAnnotationIntrospector(pair); mapper.getSerializationConfig().setAnnotationIntrospector(pair); // Set up the provider JacksonJaxbJsonProvider jaxbProvider = new JacksonJaxbJsonProvider(); jaxbProvider.setMapper(mapper); s.add(jaxbProvider); return s; } } 

And in my Employee class, I have:

 package com.dummy.example.server.services; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.json.simple.JSONObject; @Path("/services/employee") @Produces(MediaType.APPLICATION_JSON) @SuppressWarnings("unchecked") public class Employee { @GET public JSONObject get() { JSONObject json = new JSONObject(); json.put("Name", "Example"); return json; } } 

Any ideas? For several seconds I hit my head about it.

+4
source share
2 answers

The url-pattern parameter for your servlet (in the web.xml file) is independent of the path specified in your Employee class.

 <servlet-mapping> <servlet-name>ExampleServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> 

means your servlet is listening on / services / sub -path.

 @Path("/services/employee") 

means your REST application is listening on a sub-item / services / employee.

So, your web service maps to localhost: 8080 / example / services / services / employee (concatenation of the url template and @Path annotations).

If you want to open it on localhost: 8080 / example / services / employee with the specified url sample, you need to change the Employee class:

 @Path("employee") 
+4
source

Where would you expect /services/* leave? The web application indicates which URL patterns the web application wants to serve. This makes the application server (e.g. JBoss, GlassFish) the /services/foo/bar/whatever route for your web application. The Employee class will be called in response to the request /services/employee so that you can fulfill this request. Nothing else is registered, so /services/* should produce a 404 or 400 response. Since /services/* registered in your web application, I expect 400.

+1
source

Source: https://habr.com/ru/post/1445600/


All Articles