我想在一个用Typescript编写的React应用程序中使用带有webpack的'modules'选项的css-loader.这个例子是我的出发点(他们使用的是Babel,webpack和React).
webpack配置
var webpack=require('webpack');
var path=require('path');
var ExtractTextPlugin=require("extract-text-webpack-plugin");
module.exports={
entry: ['./src/main.tsx'],
output: {
path: path.resolve(__dirname, "target"),
publicPath: "/assets/",
filename: 'bundle.js'
},
debug: true,
devtool: 'eval-source-map',
plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({minimize: true})
],
resolve: {
extensions: ['', '.jsx', '.ts', '.js', '.tsx', '.css', '.less']
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts-loader'
},
{
test: /\.tsx$/,
loader: 'react-hot!ts-loader'
}, {
test: /\.jsx$/,
exclude: /(node_modules|bower_components)/,
loader: "react-hot!babel-loader"
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: "babel-loader"
}, {
test: …Run Code Online (Sandbox Code Playgroud) 我尝试访问一个开放的数据Web服务,它为我提供了流量信息.文档说请求必须是GET并且需要包含Accept: application/json和Content-Type: application/json.我不明白为什么他们需要Content-Type但是确定:
我试图只用Accept:Header 检索数据,但我总是得到一个415 Unsupported Media Type.现在我正在尝试这种方式(但我不确定我是否真的正确设置两个标头):
String entity = ClientBuilder.newClient().target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.get(String.class);
Run Code Online (Sandbox Code Playgroud)
如你所见,我正在使用Jersey 2.2而且我还在使用415 Unsupported Media Type.
编辑
所以我开始工作,但我不明白为什么.是不是accept(MediaType.APPLICATION_JSON)也header("Content-type","application/json")一样?
String responseEntity = ClientBuilder.newClient()
.target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.header("Content-type", "application/json")
.get(String.class);
Run Code Online (Sandbox Code Playgroud) 我正在测试一个 Spring MVC @RestController,它反过来调用外部 REST 服务。我MockMvc用来模拟 spring 环境,但我希望我的控制器能够真正调用外部服务。手动测试 RestController 工作正常(使用 Postman 等)。
我发现如果我以特定方式设置测试,我会得到一个完全空的响应(状态代码除外):
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = AnywhereController.class)
public class AnywhereControllerTest{
@Autowired
private AnywhereController ac;
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testGetLocations() throws Exception {
...
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/anywhere/locations").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(containsString("locations")))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
.andReturn();
}
Run Code Online (Sandbox Code Playgroud)
测试失败,因为内容和标题为空。然后我尝试将其添加到测试类中:
@Configuration
@EnableWebMvc
public static class TestConfiguration{
@Bean
public AnywhereController anywhereController(){
return new AnywhereController();
}
}
Run Code Online (Sandbox Code Playgroud)
另外我更改了ContextConfiguration注释(尽管我想知道这实际上是做什么的): …
javascript ×2
rest ×2
content-type ×1
java ×1
jax-rs ×1
jersey ×1
jointjs ×1
mockmvc ×1
reactjs ×1
spring ×1
spring-mvc ×1
svg ×1
typescript ×1
web-services ×1
webpack ×1