java_newbie
java_newbie

Reputation: 871

Spring + JAX-RS Jersey + JSON

faced with a problem which I don't understand what point try to solve from =( I have a simple app for REST services. As I use Spring @Autowired in service-classes I had to integrate it with Spring partially.

web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">


    <display-name>RESTful service</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- RESTful web-service -->
    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>training.rest.service</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

    <!-- SPRING CONTEXT -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/springServlet/appServlet/mvc-servlet.xml,
            /WEB-INF/db/db-cfg.xml,
            /WEB-INF/springServlet/application-security.xml
        </param-value>
    </context-param>


    <!-- SPRING SECURITY -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- other -->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

</web-app>

With such web.xml config I can use service methods which returns objects in JSON format like:

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public User readUser() {
    User user = new User();
    user.setLogin("login QQ");
    user.setPassword("passw QQ");
    return user;
}

but if I want to use method similar to:

@GET
@Path("{login}")
@Produces(MediaType.APPLICATION_JSON)
public User readUser(@PathParam("login") String login) throws SQLException {
    User user = userDao.findByLogin(login);
    return user;
}

I need to have userDao autowired in that service class. But it can only be correctly autowired if I change Servlet Dispatcher in web.xml from

com.sun.jersey.spi.container.servlet.ServletContainer

to

com.sun.jersey.spi.spring.container.servlet.SpringServlet

but in that case if I try to get JSON in previous manner I will get: Internal Server Error and

Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class training.rest.entity.User, and Java type class training.rest.entity.User, and MIME media type application/json was not found
    ... 52 more

so no change is made to any other part of app. I have just changed servlet dispatcher. Of course I have bean description in context.xml..

Any hint would be appreciated. Sorry if I miss any important info, I'm new in asking questions and don\t want flood with info =)

Upvotes: 0

Views: 4204

Answers (2)

java_newbie
java_newbie

Reputation: 871

OK, so that;s the solution: I just have to annotate my User class with @JsonRootName(value = "user")

Upvotes: 0

eugen
eugen

Reputation: 5916

Have a look at Genson, it provides out of box integration with jersey, just drop the jar into your classpath and voila.

Upvotes: 1

Related Questions