本文介绍了集成spring mvc和Jersey,在查看球衣端点时获取空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的简单球衣端点:$ b​​
$ b

  @Component 
@Path(/ v1 / user)
public class UserService {

@Autowired
com.myapp.core.service.UserService userService;

@GET
@Path(/ get)
public Response getUser(){
User user = userService.getUser(1);

String result = user.getUsername();

return Response.status(200).entity(result).build();






$(bb,因为这是安稳的,一个xml或json输出??)

我的spring mvc页面正常工作(即打hibernate),但是当我访问localhost:8080 / api / v1 / user / get我看到:

  java.lang.NullPointerException 
com.myapp.api.UserService.getUser(UserService.java: 30)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java :25)
java.lang.reflect.Method.invoke(Method.java:597)
com.sun.jersey.spi.container.JavaMethodInvokerFactory $ 1.invoke(JavaMethodInvokerFactory.java:60)
com.sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider $ ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
com.sun.jersey.server.impl.model .method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
com.sun .jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule .java:84)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest (WebApplicationImpl.java:1400)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
com.sun.jersey.server.impl.application.WebApplicationImpl .handl eRequest(WebApplicationImpl.java:1339)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
com.sun.jersey.spi.container.servlet。 ServletContainer.service(ServletContainer.java:537)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
javax.servlet.http.HttpServlet.service( HttpServlet.java:717)

我的web.xml:

 <?xml version =1.0encoding =UTF-8?> 
< web-app version =2.5xmlns =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_2_5.xsd>

< display-name> myapp< / display-name>
< description> myapp< / description>

< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/spring/applicationContext.xml
/WEB-INF/spring/myapp-servlet.xml
< /参数值>
< / context-param>

< servlet>
< servlet-name> myapp< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< init-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring/myapp-servlet.xml< / param-value>
< / init-param>
1< / load-on-startup>
< / servlet>

< servlet-mapping>
< servlet-name> myapp< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>


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

<! - <监听者> - >
<! - < listener-class> org.springframework.web.context.request.RequestContextListener< / listener-class> - >
<! - < / listener> - >

< servlet>
< servlet-name> jersey-servlet< / servlet-name>
< servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
< / servlet-class>
< init-param>
< param-name> com.sun.jersey.config.property.packages< / param-name>
< param-value> com.myapp.api< / param-value>
< / init-param>
<载入启动> 2< /载入启动>
< / servlet>

< servlet-mapping>
< servlet-name> jersey-servlet< / servlet-name>
< url-pattern> / api / *< / url-pattern>
< / servlet-mapping>
<! - / jersey - >

< / web-app>

我的应用程序上下文包含bean:



dataSource,sessionFactory和userDao。



我的myapp-servlet.xml包含:

 < context:component-scan base-package =com.myapp/> 

(applicationContext和myapp-servlet.xml具有相同的xml头节点等, )

解决方案

确保您的myapp-servlet.xml包含以下内容:

 <! - 启用autowire  - > 
< context:annotation-config />

关于媒体类型的请求/响应 - 您可以显式指定它,如下所示:

  @Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path( / v1 / user)
public class UserService {...

默认值为*/* - 任何类型。
有关运动衫注释的实用信息,请参阅

My simple jersey endpoint:

@Component
@Path("/v1/user")
public class UserService {

    @Autowired
    com.myapp.core.service.UserService userService;

    @GET
    @Path("/get")
    public Response getUser() {
        User user = userService.getUser(1);

        String result = user.getUsername();

        return Response.status(200).entity(result).build();
    }
}

(BTW, since this is restful, will this be an xml or json output??)

My spring mvc pages working fine (that hit hibernate), but when I visit localhost:8080/api/v1/user/get I see:

java.lang.NullPointerException
    com.myapp.api.UserService.getUser(UserService.java:30)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

My web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <display-name>myapp</display-name>
    <description>myapp</description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/spring/applicationContext.xml
        /WEB-INF/spring/myapp-servlet.xml
        </param-value>
    </context-param>

    <servlet>
        <servlet-name>myapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/myapp-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>myapp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


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

    <!--<listener>-->
          <!--<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>-->
   <!--</listener>-->

    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.spring.container.servlet.SpringServlet
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.myapp.api</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
    <!-- /jersey -->

</web-app>

My application context has beans for:

dataSource, sessionFactory and userDao.

My myapp-servlet.xml has:

<context:component-scan base-package="com.myapp" />

(both applicationContext and myapp-servlet.xml have the same xml header node etc. in case that is an issue?)

解决方案

Make sure your myapp-servlet.xml contains the following:

<!--  enable autowire -->
<context:annotation-config />

About media-type of request/response - you can specify it explicitly, like this:

@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/v1/user")
public class UserService {...

Default value is "*/*" - any type.Useful info about jersey annotation can be found here

这篇关于集成spring mvc和Jersey,在查看球衣端点时获取空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:28