小编Tap*_*ena的帖子

Not-null属性引用瞬态值 - 必须在当前操作之前保存瞬态实例

我有2个域模型和一个Spring REST控制器,如下所示:

@Entity
public class Customer{

@Id
private Long id;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;

// other stuff with getters/setters

}

@Entity
public class Country{

@Id
@Column(name="COUNTRY_ID")
private Integer id;

// other stuff with getters/setters

}
Run Code Online (Sandbox Code Playgroud)

Spring REST控制器:

@Controller
@RequestMapping("/shop/services/customers")
public class CustomerRESTController {

   /**
    * Create new customer
    */
    @RequestMapping( method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public com.salesmanager.web.entity.customer.Customer createCustomer(@Valid @RequestBody   Customer customer, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        customerService.saveOrUpdate(customer);

        return customer;
    }

    // other stuff
} …
Run Code Online (Sandbox Code Playgroud)

java rest spring json hibernate

35
推荐指数
2
解决办法
6万
查看次数

通过反射向类添加新方法

是否可以通过java中的反射向类添加方法?

public class BaseDomain {

    public BaseDomain(){
        Field[] fields = this.getClass().getDeclaredFields();
        for(int i=0; i<fields.length; i++){
            String field = fields[i].toString();

            String setterMethod = "public void set" + field.toLowerCase();

            //Now I want to add this method to this class.

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java reflection

7
推荐指数
1
解决办法
2万
查看次数

在Spring中向单个服务注入多个实现

我有一个名为DataSource的servive接口和多个实现,如XMLDataSource,DataBaseDataSource等.

我想基于一些用户交互向我的Struts2 Action注入(Spring)适当的实现,比如用户点击XML然后我需要使用XML实现.Spring已被用于DI框架.

@Autowired
private DataSource dataSource;
Run Code Online (Sandbox Code Playgroud)

请建议实现这一目标的最佳方法.

java spring struts2

1
推荐指数
2
解决办法
8347
查看次数

标签 统计

java ×3

spring ×2

hibernate ×1

json ×1

reflection ×1

rest ×1

struts2 ×1