2015-01-21 58 views
0

我使用SpringFramework(STS)开发web服务。我正在处理2个项目:webservice/backend和客户端(前端)。我的问题是,当我尝试从服务器发送一个实体对象到客户端时,使用具有参数的url,我在网页上收到错误400,但在控制台上没有错误。我得到没有错误的对象列表,但当它的实体我需要它不起作用。知道当我直接在后端调用URL时,我得到了我需要的数据。唯一的问题是将它们发送到前端。Spring mvc从服务器发送实体到客户端

下面的代码服务器端:

我的实体:UserProfile.java

@Entity 
@Table(name="user_profile") 
public class UserProfile { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name="id", nullable = false) 
    private int id; 

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) 
    @JoinColumn(name="id_user") 
    private User user; 

    @Column(name = "coverPhoto", nullable = false) 
    private String coverPhoto; 

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) 
    @JoinColumn(name="id_modelLibrary") 
    private ModelLibrary modelLibrary; 

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) 
    @JoinColumn(name="id_shop") 
    private Shop shop; 

    public UserProfile(){} 

    public UserProfile(User user){ 
     this.user = new User(); 
     this.setCoverPhoto(""); 
     this.setUser(user); 
     this.setModelLibrary(new ModelLibrary(user)); 
     this.setShop(new Shop(user)); 
    } 
    // Getters & setters 
} 

控制器(服务器端):UserProfileController.java

@RestController 
public class UserProfileController { 
    private UserProfileService userProfileService; 

    // Recuperer les infos de session 
    @RequestMapping(value = "/getProfile/{id}", method = RequestMethod.GET) 
    public UserProfile getMyProfile(@PathVariable("id") String userId) { 
     Application.context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
     userProfileService = (UserProfileService) Application.context.getBean("userProfileService"); 
     UserProfile userProfile = userProfileService.getUserProfileByUserId(userId); 
     System.out.println(userProfile.getUser().toString()); 
     return userProfile; 
    } 
} 

而且现在的代码客户端: UserProfileController.java

@Controller 
public class UserProfileController { 

    private UserProfileService uProfileService = new UserProfileServiceImpl(); 
    private UserProfile uprofile; 

     @RequestMapping(value = "/myProfile", method = RequestMethod.GET) 
     public String getMyProfile(HttpSession session, Model model){ 
      User user = (User) session.getAttribute("Sessionuser"); 
      UserProfile uProfile = this.uProfileService.getUserProfileByUserId(user.getId()); 
      this.uprofile = uProfile; 
      System.out.println("Get profile "+ uProfile.getUser().getUsername()); 
      model.addAttribute("userProfile", this.uprofile); 
      return "myprofile"; 
     } 
} 

(该UserProfileService只返回的DAO方法的结果,所以无需输入。)

的UserProfileDAO.java(客户端)

@Repository("userProfileDAO") 
public class UserProfileDAOImpl implements UserProfileDAO{ 


    @Override 
    public UserProfile getUserProfileByUserId(String user_id) { 
     return new RestTemplate().getForObject(WebService.getWebServiceUrl() + "getProfile/" + user_id, UserProfile.class); 

    } 

} 

回答

0

使用spring ModelAndView.addObject() method

请尝试以下代码:

model.addObject("userProfile", this.uprofile); 

而不是:

model.addAttribute("userProfile", this.uprofile); 
+0

谢谢你的回答。我尝试过,但仍然无法正常工作。在调试后,我发现有问题来自 返回新的RestTemplate()。getForObject(WebService.getWebServiceUrl()+“getProfile /”+ user_id,UserProfile.class); '因为我得到的错误弹出窗口之后,打印后不打印... – mika 2015-01-21 11:08:05

+0

好吧,我没有看到它,但addObject()是这样做的最好方法,它工作正常为了我。 – 2015-01-21 11:25:31

相关问题